LMB.PredicateBuilderExtension 1.0.0

Predicate Builder Extension

Enables the efficient, dynamic composition of query predicates linq expressions supporting the logical operators: AND, OR, NOT.

It can be used with any kind of entity that need to be queried by a linq expression.

Install-Package LMB.PredicateBuilderExtension -Version 1.0.0
dotnet add package LMB.PredicateBuilderExtension --version 1.0.0
<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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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&lt;Func&lt;T,bool&gt;&gt;), 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&lt;T&gt; 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&lt;Func&lt;Person, bool&gt;&gt; True&lt;Person&gt;()

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&lt;Person&gt; 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.

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&lt;Func&lt;T,bool&gt;&gt;), 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&lt;T&gt; 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&lt;Func&lt;Person, bool&gt;&gt; True&lt;Person&gt;()

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&lt;Person&gt; 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.

Dependencies

This package has no dependencies.

Version History

Version Downloads Last updated
1.0.0 582 10/2/2017