c# - Get AND of two Expression<Predicate<string>> objects -
i need build expression> dynamically conjunction of expression> objects, e.g.
expression<predicate<string>> expression1 = c => c.contains("aaa"); expression<predicate<string>> expression2 = c => c.contains("bbb");
how can combine these expressions , operator obtain
expression<predicate<string>>
object?
thanks!
var par = expression.parameter(typeof(string)); expression<predicate<string>> expression3 = expression.lambda<predicate<string>>( expression.andalso( expression.invoke(expression1, par), expression.invoke(expression2, par)), par); predicate<string> method1 = expression3.compile(); console.writeline(method1("aaa")); console.writeline(method1("bbb")); console.writeline(method1("aaabbb"));
we have create new expression. par
parameter. use expression.invoke
invoke "old" expressions (and passing par
parameter). use expression.andalso
make &&
expression. use expression.lambda<predicate<string>>
create final expression (the last parameter, par
name of parameter of lambda expression)
be aware (common error) expression.and
&
, while expression.andalso
&&
(the same expression.or*
)
Comments
Post a Comment