Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does PredicateBuilder work

C# in a Nutshell has a free class called PredicateBuilder which constructs LINQ predicates piece by piece available here. Here's an extract of the method which adds a new expression to the predicate. Could someone explain it? (I have seen this question, I don't want a general answer like there. I am looking for a specific explanation of how Expression.Invoke and Expression.Lambda build the new expression).

public static Expression<Func<T, bool>> And<T> (this Expression<Func<T, bool>> expr1,                                                      Expression<Func<T, bool>> expr2) {     var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast<Expression> ());     return Expression.Lambda<Func<T, bool>>         (Expression.AndAlso (expr1.Body, invokedExpr), expr1.Parameters); } 
like image 355
just.another.programmer Avatar asked Jul 15 '12 09:07

just.another.programmer


People also ask

What is PredicateBuilder?

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. We can write a query like Dynamic SQL. To learn more about predicate delegate visit Predicate Delegate.

What is LINQKit?

LINQKit is a free set of extensions for LINQ to SQL and Entity Framework power users. It comprises the following: An extensible implementation of AsExpandable() A public expression visitor base class (ExpressionVisitor) PredicateBuilder.

What is predicate in LINQ C#?

A predicate, or more precisely a predicate functor, is a Boolean-valued function. It is often a unary function, checking one argument against a condition and returning the result. Disregarding functions depending on side effects, there are two nullary functions (without arguments). public static class Predicate.

What is AsExpandable?

AsExpandable() (and . Expand() ) does is walks the expression tree and swaps out any instances of . Invoke() with the actual expression tree its invoking, before EF ever gets to it. In this way, EF only sees a "vanilla" expression tree with no . Invoke() s.


1 Answers

Let's say you have:

Expression<Func<Person, bool>> isAdult = p1 => p1.Age >= 18;  // I've given the parameter a different name to allow you to differentiate. Expression<Func<Person, bool>> isMale = p2 => p2.Gender == "Male"; 

And then combine them with PredicateBuilder

var isAdultMale = isAdult.And(isMale); 

What PredicateBuilder produces is an expression that looks like this:

// Invoke has no direct equivalent in C# lambda expressions. p1 => p1.Age >= 18 && Invoke(p2 => p2.Gender == "Male", p1) 

As you can see:

  1. The resulting lambda reuses the parameters of the first expression.
  2. Has a body that invokes the second expression by passing the parameters of the first expression as a replacement for the second expression's parameters. The resulting InvocationExpression is sort of like the expression-equivalent of a method-call (calling a routine by passing in arguments for parameters).
  3. Ands the first expression's body and this InvocationExpression together to produce the body of the resulting lambda.

The idea is that the LINQ provider should be able to understand the semantics of this operation and take a sensible course of action (e.g. generate SQL like WHERE age >= 18 AND gender = 'Male').

Often though, providers have problems with InvocationExpressions, because of the obvious complications of processing a 'nested expression-call inside an expression.'

To get around this, LINQKit also provides the Expand helper. This essentially 'inlines' the invocation call smartly by replacing the call with the body of the nested expression, substituting uses of the nested expression's parameters appropriately (in this case, replacing p2 with p1). This should produce something like:

p1 => p1.Age >= 18 && p1.Gender == "Male" 

Note that this how you would have manually combined those predicates if you'd done it yourself in a lambda. But with LINQKit around, you can get these predicates from independent sources and easily combine them:

  1. Without writing "by hand" expression code.
  2. Optionally, in a way that is transparent to consumers of the resulting lambda.
like image 55
Ani Avatar answered Sep 22 '22 04:09

Ani