Linq.Fluent.PredicateBuilder 1.0.1

.NET Standard 2.0
NuGet\Install-Package Linq.Fluent.PredicateBuilder -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
dotnet add package Linq.Fluent.PredicateBuilder --version 1.0.1
<PackageReference Include="Linq.Fluent.PredicateBuilder" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Linq.Fluent.PredicateBuilder --version 1.0.1
#r "nuget: Linq.Fluent.PredicateBuilder, 1.0.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Linq.Fluent.PredicateBuilder as a Cake Addin
#addin nuget:?package=Linq.Fluent.PredicateBuilder&version=1.0.1

// Install Linq.Fluent.PredicateBuilder as a Cake Tool
#tool nuget:?package=Linq.Fluent.PredicateBuilder&version=1.0.1

Linq.Fluent.PredicateBuilder

Fluent Predicate Builder

Basically, you have to provide a predicate to the Linq Where method.

Using the fluent PredicateBuilder, you can perform

  • And
  • AndAlso
  • Or
  • OrElse

operations, to build your predicate.

In below example, an item is matched if any of the 3 predicates match.

var list = Enumerable.Range(1, 10000000);

var predicate = new PredicateBuilder<int>()
                        .Initial(x => x >= 12 && x <= 13)
                        .Or(x => x <= 2)
                        .Or(x => x > 99999999)
                .ToPredicate();

var result = list.Where(predicate)
                 .ToList();

Assert.Equal(4, result.Count());

Also, You can create predicates based on conditions.

Eg. In below query, all employees are returned if restrict is false.

Only 1 employee is returned if restrict is true.

[Theory(DisplayName = "Test_Fluent_PredicateBuilder_Conditional")]
[InlineData(true)]
[InlineData(false)]
public void Test_Fluent_PredicateBuilder_Conditional(bool restrict)
{
    var list = new List<Employee>
    {
        new Employee { Id = 1, Name = "Cliff", Department = "Human Resources"},
        new Employee { Id = 2, Name = "John", Department = "IT"}
    };

    var predicate = new PredicateBuilder<Employee>()
                            .Initial(e => true)
                            .And(restrict,  e => e.Department == "IT")
                    .ToPredicate();

    var result = list.Where(predicate)
                        .ToList();

    if (restrict)
    {
        Assert.Single(result);
        Assert.Equal("John", result[0].Name);
    }                
    else
    {
        Assert.Equal(2, result.Count());
        Assert.Equal("Cliff", result[0].Name);
        Assert.Equal("John", result[1].Name);
    }                
}

Note:

ToPredicate produces a predicate (Func<T, bool>).

This can be used with IEnumerable<T>.Where.

ToExpressionPredicate produces an expression predicate (Expression<Func<T, bool>>).

This can be used with IQueryable<T>.Where

Linq Where extensions

There is also an easy to use Linq Where extensions.

Based on operation

Just select the operation and specify the predicates.

All predicates are combined as per the specified operation by the extension.

var list = Enumerable.Range(1, 10000000);

var result = list.Where(Operation.Or,
                            x => x >= 12 && x <= 13,
                            x => x <= 2,
                            x => x > 99999999)
                 .ToList();

Assert.Equal(4, result.Count());

With Predicate Builder

var list = Enumerable.Range(1, 10000000);

var result = list.Where(builder => builder.Initial(x => x >= 12 && x <= 13)
                                          .Or(x => x <= 2)
                                          .Or(x => x > 99999999)
                                    .ToPredicate())
                 .ToList();

Assert.Equal(4, result.Count());

Note:

There are 2 Where extensions each for:

  • IEnumerable<T>.Where
  • IQueryable<T>.Where
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Linq.Fluent.PredicateBuilder:

Package Downloads
AspNetCore.SignalR.EventStream

Event Sourcing framework using SignalR web sockets.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 111 7/5/2022
1.0.0 77 7/3/2022

Added conditional methods to predicate builder.