Reputation: 35
I'm making Linq code to query a database, and depending on a boolean, i need to switch between using a statement and using a !statement (ie. NOT), how can i do this without using the below if/else which would mean i have very identical code two places.
See the following example:
private string getFromDB(bool decision)
{
if (decision)
{
return db.Where(p => (Types.Contains(p.CurrentOwner)));
}
else
{
return db.Where(p => !(Types.Contains(p.CurrentOwner)));
}
}
It has to be usable for a LINQ query, and the above example is simplified down to the essence of the problem.
Thanks in advance!
Upvotes: 1
Views: 148
Reputation: 3063
private string getFromDB(bool decision)
{
return db.Where(p => (Types.Contains(p.CurrentOwner) == decision));
}
Upvotes: 5