Beatles1692
Beatles1692

Reputation: 5320

Is there a way to use || or && operators between expressions

I'm trying to refactor a complex Linq query. I would like to know if there's a way that we can do something like this : Suppose that we have two expressions:

Expression<Func<object,bool>> exp1=....;
Expression<Func<object,bool>> exp2=....;

Expression<Func<object,bool>> exp3=exp1 || exp2;

Upvotes: 3

Views: 803

Answers (2)

Marc Gravell
Marc Gravell

Reputation: 1062770

Basically you need to re-write one of the trees, so that you can combine the expressions. Fortunately ExpressionVisitor helps us:

static void Main()
{
    Expression<Func<object, bool>> exp1 = x => ((string)x).StartsWith("a");
    Expression<Func<object, bool>> exp2 = y => ((string)y).StartsWith("b");
    // the two expressions here are purely for illustration

    var exp3 = Combine(exp1, exp2, Expression.OrElse); // i.e. ||
    var exp4 = Combine(exp1, exp2, Expression.AndAlso); // i.e. &&
}
static Expression<Func<TValue, TResult>> Combine<TValue, TResult>(
    Expression<Func<TValue, TResult>> left,
    Expression<Func<TValue, TResult>> right,
    Func<Expression, Expression, BinaryExpression> combination)
{
    // rewrite the body of "right" using "left"'s parameter in place
    // of the original "right"'s parameter
    var newRight = new SwapVisitor(right.Parameters[0], left.Parameters[0])
                        .Visit(right.Body);
    // combine via && / || etc and create a new lambda
    return Expression.Lambda<Func<TValue, TResult>>(
        combination(left.Body, newRight), left.Parameters);
}
class SwapVisitor : ExpressionVisitor {
    private readonly Expression from, to;
    public SwapVisitor(Expression from, Expression to) {
        this.from = from;
        this.to = to;
    }
    public override Expression Visit(Expression node) {
        return node == from ? to : base.Visit(node);
    }
}

Note that this also allows other arbitrary uses, for example:

Expression<Func<object, int>> exp1 = x => x.GetHashCode();
Expression<Func<object, int>> exp2 = y => y.ToString().Length;

var exp3 = Combine(exp1, exp2, Expression.Add);
// which is: x => x.GetHashCode() + x.ToString().Length;

I can think of no practical use for that ... but it is there ;p

Upvotes: 6

Loetn
Loetn

Reputation: 4030

You can use Dynamic Linq Query or PredicateBuilder.

Upvotes: 1

Related Questions