Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining Func<bool> expressions [duplicate]

Possible Duplicate:
Combining two expressions (Expression<Func<T, bool>>)

I have a method taking in a single Expression<Func<bool>> parameter

void MethodOne(Expression<Func<bool>> expression)

I've got multiple instances of Expression<Func<bool>>. How do I dynamically combine these expressions into a single Expression<Func<bool>> using Expression.OrElse (i.e. building up an expression tree)?

For example if I have two expressions such as

() => objectA.PropertyOneIsSet

and

() => objectB.PropertyTwoIsSet

I want the end result to be:

() => objectA.PropertyOneIsSet || objectB.PropertyTwoIsSet

so I can pass this to my method above.

like image 661
alex.tashev Avatar asked Nov 23 '12 14:11

alex.tashev


2 Answers

You can create ExpressionVisitor to combine queries. Check this msdn blog for more info: Combining Predicates (Answer 3). He talking about EF, but you can use it in your case

like image 128
Uriil Avatar answered Oct 16 '22 12:10

Uriil


You could use expressions.Any(x => x.CallMethod) in order to achieve this goal.

like image 3
Damyan Bogoev Avatar answered Oct 16 '22 11:10

Damyan Bogoev