I am trying to dynamically create a linq expression at runtime using PredicateBuilder from http://www.albahari.com/nutshell/predicatebuilder.aspx.
I currently have a method that takes a list of criteria objects, and then parses those into multiple predicates much as described in this post.
So currently, my code supports the following scenario:
WHERE
((a == <val1>) AND (b == <val2>) AND (c == <val3>))
OR
((a == <val4>) AND (b == <val2>) AND (c == <val3>))
But I need it to work like this:
WHERE
((a == <val1> OR a == <val4>) AND (b == <val2>) AND (c == <val3>))
OR
((a == <val7>) AND (b == <val5>) AND (c == <val6>))
How can I make it so that I can “group” two “ORs” together so the logic flows properly? I don’t want “a OR a AND b AND c”, I need “(a OR a) AND b and C”.
The “dynamic” aspect of these predicates isn’t clear at all. Why bother with
PredicateBuilderwhen you could assemble the required expression with&&and||expressions (with proper parentheses of course)?In any case, here’s a way to accomplish what you want with
PredicateBuilder:The idea is to create the individual “simple” expressions, and then finally
ORthem together to produce the final predicate.