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

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -