Search code examples
c#.netexpression-treeslinqpadpredicatebuilder

PredicateBuilder methods clarification


I looked through PredicateBuilder sources and its' implementation makes me curious. Let's look at Or method implementation:

public static Expression<Func<T, bool>> Or<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.OrElse (expr1.Body, invokedExpr), expr1.Parameters);
  }

Why does it Invoke new lambda instead of just using OrElse for predicate body?


Solution

  • I believe it's a typing issue: Expression.OrElse returns a plain Expression, not an Expression<Func<T, bool>>.