Accepted answer

Scott's answer is great; I'd just like to point out that you can in fact do this query using query continuation syntax:

from parent in parents 
where parent.Children.Any (c => c.CategoryNumber==1)
select parent into p
from child in p.Children
where child.CategoryNumber == 2
select child

Notice how the "into" lets you pipe the result of one query into the next query. Pretty slick, eh?


You can string a couple queries together, using SelectMany and Where.

var selectedChildren = (from p in parents
                       where p.Children.Any (c => c.CategoryNumber==1)
                       select p)
                       .SelectMany(p => p.Children)
                       .Where(c => c.CategoryNumber == 2);

// or...

var selectedChildren = parents
                         .Where(p => p.Children.Any(c => c.CategoryNumber == 1))
                         .SelectMany(p => p.Children)
                         .Where(c => c.CategoryNumber == 2);

Related Query

More Query from same tag