8

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.

8
  • you could try to implement it yourself - but you'd need to manipulate expression-trees - if you really have this much "or" clauses you might wonder if there is a smell in there
    – Random Dev
    Jul 2, 2014 at 17:03
  • btw: from a syntax standpoint: just break the lines - it should not look this much worse than a new .Where( each line (maybe even better)
    – Random Dev
    Jul 2, 2014 at 17:04
  • 2
    you could take a look at using Queryable.Concat(IQueryable<T>, IQueryable<T>) it runs much faster than Union, but means that each queryable could produce duplicates of items...Concat equates to Union ALL and Union equates to Union.
    – Aron
    Jul 2, 2014 at 17:06
  • 2
    The callable you're passing to 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. Jul 2, 2014 at 17:08
  • 1
    if r.[Property] is a bool and there are a lot of bools getting compared then you need to tweak the design a bit, please consider using Enum (Attributed with Flags?) . Jul 2, 2014 at 17:14

1 Answer 1

12

Use PredicateBuilder. http://www.albahari.com/nutshell/predicatebuilder.aspx

It's a dozen lines and it lets you do this:

Expression<...> filter = PredicateBuilder.False();
if (something)
    filter = filter.Or(r => r.Foo);
if (somethingElse)
    filter = filter.Or(r => r.Bar);
return results.Where(filter);

This gives you all the same performance benefits as if you had written:

if (something && somethingElse)
    return results.Where(r => r.Foo || r => r.Bar)
else if (something)
    return results.Where(r => r.foo)
else if (somethingElse)
    return results.Where(r => r.Bar)
etc... 
2
  • I have seen this, but I need to stay away from 3rd party libraries in this case. This is what I want though, so upvoting for future readers.
    – Evan
    Jul 2, 2014 at 20:00
  • 1
    Only gives you the same performance in SQL not in C#, building expressions dynamically takes much longer.
    – bto.rdz
    Aug 4, 2018 at 23:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.