Reputation: 26981
Let's say I have a query like:
r = r.Where(x => iEnumerableMachineNames.Contains(x.Machine.Name)
|| x.Server==true);
Is there a way to build the predicate (I think that's what it's called) outside of the statement, for instance
t = (x => iEnumerableMachineNames.Contains(x.Machine.Name));
s = (x => x.Server==true)
q = t.Or(s);
r = r.Where(x => q);
Basically I want to programmatically build my query based on input parameters, for EF 5.
Upvotes: 4
Views: 102
Reputation: 16543
You can build expressions dynamically but not as simply as your pseudo code - it requires reflection and expression trees (read this).
A simple way to accomplish what it seems like you'd like to do is to short circuit different parts of the predicate using boolean flags:
bool testMachineName;
bool testIsServer;
r = r.Where( x =>
( !testMachineName || iEnumerableMachineNames.Contains( x.Machine.Name ) ) ||
( !testIsServer || x.Server ) );
Upvotes: 1