Edit - using IQueryable, not an in memory collection.
So I basically want to tack on Where clauses based on some flags, but not have them act as ANDs. So let's say I want results where "Foo" is true Or "Bar" is true. I'm currently doing this...
results = results.Where(r => r.Foo || r.Bar);
which is fine, but there's a lot of conditions that I need to add ORs to and if I could chain them after each other it would make the code much more readable.
Is there any SIMPLE way to do it like the following, but using OR instead of AND.
if (something)
results = results.Where(r => r.Foo)
if (somethingElse)
results = results.Where(r => r.Bar)
It's not really worth it to me if I have to build Expression objects and stuff like that, or use a 3rd party library, just wondering if there was something simple I wasn't seeing.
Also, I thought about using Union()
, but it seemed to perform slower(although it was more readable).
Some more info that could help. Basically my conditions are user roles. And they can be in any combination of roles, but each one should add an OR condition to the "results". So yeah, the enum flag thing that someone mentioned could help here.
.Where(
each line (maybe even better)Queryable.Concat(IQueryable<T>, IQueryable<T>)
it runs much faster thanUnion
, but means that each queryable could produce duplicates of items...Concat
equates toUnion ALL
andUnion
equates toUnion
.Where
can be as large as you want, so you can use whatever syntax you like to produce the boolean that determines whether each element stays in the filtered enumerable. You're not limited to a single expression, you can have multiple statements.