Reputation: 1593
I've got some IQueryables I would like to OR together. My understanding is chaining them together using Where
results in ANDing them together (though my understanding may be faulty.)
Here's an example of a query I would like to OR together, in lieu of using all the chained Where
clauses:
var query = query.Where(x => x.Value == 1)
.Where(x => x.Name == name);
Instead of that, I'm looking for something like:
var query = query.Where(x => x.Value == 1)
.Or(x => x.Name == name)
.Or(x => x.SomeOtherValue == something else)
.Or(x => x.AChildObject.Items.Where(item => item.SomeValue == yet something else));
...in my case there are probably 10 or items I'd need to chain together like above.
I've been looking over posts like this one and I suppose I could use a series of ||
statements to chain things together, but am not sure if that's the way to go. It could get very hard to read.
In researching this online I'm running into meaty articles on PredicateBuilders and the like. I'm not an expert on LINQ by any means and I was hoping for some guidance?
Upvotes: 0
Views: 647
Reputation: 1221
It looks like that you can just use OR operator.
var query = query.Where(x => x.Value == 1
|| x.Name == name)
|| x => x.SomeOtherValue == something else)
|| x => x.AChildObject.Items.Where(item => item.SomeValue == yet something else))
You can also use Union or Concat if the data being combined from different sources, see Linq union usage?
Upvotes: 2