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.
To complete Eric's answer, using the new
ExpressionVisitor
introduced in .NET 4 rather than a custom rewriter: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:
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#