Since SQL does not short-circuits OR
s, you can convert an expression tree that looks like this
OR
\
OR
\
OR
\
OR
to an expression tree that looks like this:
OR
/ \
/ \
/ \
OR OR
/ \ / \
OR OR OR OR
This is only a work-around: ideally, the framework should be able to deal with situations like that.
One way to construct a tree like this is to split your list recursively in halves, construct an "OR
-tree" from each half recursively, and then combine the two "OR
-tree"s with another OR
:
Predicate ToOrTree(List<Foo> fooList) {
if (fooList.Count > 2) {
var firstHalf = fooList.Count / 2;
var lhs = ToOrTree(fooList.Take(firstHalf).ToList());
var rhs = ToOrTree(fooList.Skip(firstHalf).ToList());
return lhs.Or(rhs);
}
Predicate res = PredicateBuilder.Create<Foo>(
foo => fooList[0].Bar == foo.Bar && fooList[0].Baz == foo.Baz
);
if (fooList.Count == 2) {
res = res.Or(
foo => fooList[1].Bar == foo.Bar && fooList[1].Baz == foo.Baz
);
}
return res;
}