Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Please find the solution in the blog post below:</p> <p><a href="http://petemontgomery.wordpress.com/2011/02/10/a-universal-predicatebuilder/" rel="nofollow">A universal PredicateBuilder</a></p> <p>The author of this blog post provide another to implementation of "Add" method to combine 2 expression.</p> <pre><code>/// &lt;summary&gt; /// Enables the efficient, dynamic composition of query predicates. /// &lt;/summary&gt; public static class PredicateBuilder { /// &lt;summary&gt; /// Creates a predicate that evaluates to true. /// &lt;/summary&gt; public static Expression&lt;Func&lt;T, bool&gt;&gt; True&lt;T&gt;() { return param =&gt; true; } /// &lt;summary&gt; /// Creates a predicate that evaluates to false. /// &lt;/summary&gt; public static Expression&lt;Func&lt;T, bool&gt;&gt; False&lt;T&gt;() { return param =&gt; false; } /// &lt;summary&gt; /// Creates a predicate expression from the specified lambda expression. /// &lt;/summary&gt; public static Expression&lt;Func&lt;T, bool&gt;&gt; Create&lt;T&gt;(Expression&lt;Func&lt;T, bool&gt;&gt; predicate) { return predicate; } /// &lt;summary&gt; /// Combines the first predicate with the second using the logical "and". /// &lt;/summary&gt; public static Expression&lt;Func&lt;T, bool&gt;&gt; And&lt;T&gt;(this Expression&lt;Func&lt;T, bool&gt;&gt; first, Expression&lt;Func&lt;T, bool&gt;&gt; second) { return first.Compose(second, Expression.AndAlso); } /// &lt;summary&gt; /// Combines the first predicate with the second using the logical "or". /// &lt;/summary&gt; public static Expression&lt;Func&lt;T, bool&gt;&gt; Or&lt;T&gt;(this Expression&lt;Func&lt;T, bool&gt;&gt; first, Expression&lt;Func&lt;T, bool&gt;&gt; second) { return first.Compose(second, Expression.OrElse); } /// &lt;summary&gt; /// Negates the predicate. /// &lt;/summary&gt; public static Expression&lt;Func&lt;T, bool&gt;&gt; Not&lt;T&gt;(this Expression&lt;Func&lt;T, bool&gt;&gt; expression) { var negated = Expression.Not(expression.Body); return Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(negated, expression.Parameters); } /// &lt;summary&gt; /// Combines the first expression with the second using the specified merge function. /// &lt;/summary&gt; static Expression&lt;T&gt; Compose&lt;T&gt;(this Expression&lt;T&gt; first, Expression&lt;T&gt; second, Func&lt;Expression, Expression, Expression&gt; merge) { // zip parameters (map from parameters of second to parameters of first) var map = first.Parameters .Select((f, i) =&gt; new { f, s = second.Parameters[i] }) .ToDictionary(p =&gt; p.s, p =&gt; p.f); // replace parameters in the second lambda expression with the parameters in the first var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body); // create a merged lambda expression with parameters from the first expression return Expression.Lambda&lt;T&gt;(merge(first.Body, secondBody), first.Parameters); } class ParameterRebinder : ExpressionVisitor { readonly Dictionary&lt;ParameterExpression, ParameterExpression&gt; map; ParameterRebinder(Dictionary&lt;ParameterExpression, ParameterExpression&gt; map) { this.map = map ?? new Dictionary&lt;ParameterExpression, ParameterExpression&gt;(); } public static Expression ReplaceParameters(Dictionary&lt;ParameterExpression, ParameterExpression&gt; map, Expression exp) { return new ParameterRebinder(map).Visit(exp); } protected override Expression VisitParameter(ParameterExpression p) { ParameterExpression replacement; if (map.TryGetValue(p, out replacement)) { p = replacement; } return base.VisitParameter(p); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload