LMB.PredicateBuilderExtension 1.0.0

dotnet add package LMB.PredicateBuilderExtension --version 1.0.0
NuGet\Install-Package LMB.PredicateBuilderExtension -Version 1.0.0
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="LMB.PredicateBuilderExtension" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LMB.PredicateBuilderExtension --version 1.0.0
#r "nuget: LMB.PredicateBuilderExtension, 1.0.0"
#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 LMB.PredicateBuilderExtension as a Cake Addin
#addin nuget:?package=LMB.PredicateBuilderExtension&version=1.0.0

// Install LMB.PredicateBuilderExtension as a Cake Tool
#tool nuget:?package=LMB.PredicateBuilderExtension&version=1.0.0

Predicate Builder Extension

Predicate Builder is a powerful LINQ expression that is mainly used when too many search filter parameters are used for querying data by writing dynamic query expression.

Using the Predicate Builder we can create LINQ to SQL dynamic query and Query with Entity Framework is easy.

How it works?

This is using Expression Trees to "build" a predicate from two input expressions representing predicates.

Expression Trees are a way to use lambda's to generate a representation of code in a tree like structure (rather than a delegate directly). This takes two expression trees representing predicates (Expression<Func<T,bool>>), and combines them into a new expression tree representing the "or" case (or the "and" case).

Expression trees, and their corresponding utilities like above, are useful for things like ORMs. For example, Entity Framework uses expression trees with IQueryable<T> to turn "code" defined as a lambda into SQL that's run on the server.

How to use this extension?

For this example, we will use the Person class:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

And a Collection of persons...

public ICollection<Person> Persons = new List<Person>
{
    { new Person {   Name  = "Albert", Age = 31 } },
    { new Person {   Name  = "Amanda" , Age = 18 } },
    { new Person {   Name  = "Angelina" , Age = 23 } },
    { new Person {   Name  = "Bishop", Age = 28 } },
    { new Person {   Name  = "Brad", Age = 35 } },
    { new Person {   Name  = "Dilbert", Age = 50 } },
    { new Person {   Name  = "Lilith", Age = 8 } },
    { new Person {   Name  = "Lindsey", Age = 29 }  },
    { new Person {   Name  = "Tom", Age = 30 }  }
};

Step 1 - Create a predicate expression:

With the code below, we are creating a predicate variable of type Expression<Func<Person, bool>> True<Person>()

var predicate = PredicateBuilderExtension.True<Person>();

Step 2 - Add your flavored predicates:

predicate = predicate.And(x => x.Name.StartsWith("A"));
predicate = predicate.Or(x => x.Name.StartsWith("B"));

Here we are adding a logical operator AND (predicate.And()) to filter all persons wich name starts with the "A" character and we are adding a logical operator "OR" (predicate.Or()) to filter all persons wich name starts with the "B" character.

With the syntax above, our filter will bring all persons that name starts with "A" OR starts with "B" character.

Step 3 - Apply the predicate:

var queryResult = Persons.AsQueryable().Where(predicate).ToList();

Here we are creating a queryResult variable of type List<Person> and we are filtering the Person collection applying the predicate into the Where() clause.

Based on our person collection, the results inside the queryResult variable will be:

{ Name = "Albert",   Age = 31 }
{ Name = "Amanda" ,  Age = 18 }
{ Name = "Angelina", Age = 23 }
{ Name = "Bishop",   Age = 28 }
{ Name = "Brad",     Age = 35 }

Where to get this extension?

You can install this extension direct from Nuget:

Install-Package LMB.PredicateBuilderExtension

Or you can download this github project and copy the PredicateBuilderExtension.cs file direct into your project.

Product Compatible and additional computed target framework versions.
.NET Framework net46 is compatible.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 1,929 10/2/2017