score:7

Accepted answer

You need to use a single where

_collection.Where(x => x.ByIdA() || x.ByIdB());

score:4

LINQ probably isn't the way to solve this one. Each LINQ method acts independently, so if you managed to get your OR in a chain like this, it would difficult to read, and definitely difficult to understand and debug.

You probably just want to OR the old fashioned way:

_something.Entity.Where(x => IsA(x) || IsB(x))

score:6

To return the union of two collections, you can use the Union operator.

_something.Entity.ByIdA().Union(_something.Entity.ByIdB())

If your collections don't have any common elements, you can use the Concat operator, which is cheaper since it does not check for duplicates.

score:0

For the first part of your question could use:

var list = _something.Entity;
var result = Enumerable.Union(list.ByIdA(), list.ByIdB());

But for the second part, which seems another question, There's need to clarify the question. If you mean that each of A objects has a list of B(s) and You want all A objects which A.x==1 or A.Bs.Any(b=>b.y==1) then you could use:

var result = list.Where(a => a.x==1 || a=>a.Bs.Any(b=>b.y==1));

Related Articles