How to create linq predicate with multiple nested "ands" and "ors"

expression-trees linq predicatebuilder

Question

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".

Accepted Answer

The "dynamic" aspect of these predicates isn't clear at all. Why bother with PredicateBuilder when 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:

var p1 = PredicateBuilder.False<Foo>()
                         .Or(foo => foo.A == <val1>)
                         .Or(foo => foo.A == <val4>)
                         .And(foo => foo.B == <val2>)
                         .And(foo => foo. C == <val3>);

var p2 = PredicateBuilder.False<Foo>()
                         .Or(foo => foo.A == <val7>)
                         .And(foo => foo.B == <val5>)
                         .And(foo => foo.C == <val6>);

var finalPredicate = p1.Or(p2);

The idea is to create the individual "simple" expressions, and then finally OR them together to produce the final predicate.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why