How to Combine two lambdas [duplicate]

2019-01-10 15:21发布

问题:

Possible Duplicate:
combining two lamba expressions in c#

I have two following expressions:

Expression<Func<string, bool>> expr1 = s => s.Length == 5;
Expression<Func<string, bool>> expr2 = s => s == "someString";

Now I need to combine them with OR. Something like this:

Expression.Or(expr1, expr2)

Is there any way to make this similar to above code way like:

expr1 || expr2

I understand in this example I can just combine it in the first place:

Expression<Func<string, bool>> expr = s => s.Length == 5 || s == "someString"

but I can't do it in my real code as I get expr1 and expr2 as arguments to the method.

回答1:

To complete Eric's answer, using the new ExpressionVisitor introduced in .NET 4 rather than a custom rewriter:

internal class ParameterReplacer : ExpressionVisitor {
    private readonly ParameterExpression _parameter;

    protected override Expression VisitParameter(ParameterExpression node) {
        return base.VisitParameter(_parameter);
    }

    internal ParameterReplacer(ParameterExpression parameter) {
        _parameter = parameter;
    }
}

class Program {

    static void Main(string[] args) {
        Expression<Func<string, bool>> expr1 = s => s.Length == 5;
        Expression<Func<string, bool>> expr2 = s => s == "someString";
        var paramExpr = Expression.Parameter(typeof(string));
        var exprBody = Expression.Or(expr1.Body, expr2.Body);
        exprBody = (BinaryExpression) new ParameterReplacer(paramExpr).Visit(exprBody);
        var finalExpr = Expression.Lambda<Func<string, bool>>(exprBody, paramExpr);
    }

}


回答2:

The problem is that the "s" parameters in each lambda have the same name and same type, but they are different parameters. Parameters have reference identity, not value identity. Simply combining the two bodies of the existing expression trees into a third expression tree effectively makes:

s => s1.Length == 5 || s2 == "somestring"

which doesn't make any sense. What you want to do is write a visitor that does a search-and-replace of the parameter s with a new parameter that you will then use as the parameter to the new lambda expression.

See this related question for more details:

Combining two lambda expressions in c#