If you’re adding “and” conditions to a Linq query, it’s easy to do it like so:
var q = MyTable;
if (condition1)
q = q.Where(t => t.Field1 == value1);
if (condition2)
q = q.Where(t => t.Field2 > t.Field3);
// etc.
Is there any clever way of doing the same thing, when you want to add “or” conditions?
You can use a PredicateBuilder and use it to build an
Orbased expression:You can read more about it here:
http://www.albahari.com/nutshell/predicatebuilder.aspx
Replace the
ProductinPredicateBuilder.False<Product>()with your queried object.Note that you start from a
Falsepredicate as you want to useOr. If You’d want anAndpredicate, Yuo should start from aTrue