Linq.Fluent.PredicateBuilder 1.0.1

dotnet add package Linq.Fluent.PredicateBuilder --version 1.0.1                
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.
<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 and Polyglot Notebooks. 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 Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
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 5,865 7/5/2022
1.0.0 431 7/3/2022

Added conditional methods to predicate builder.