Accepted answer

Here's the obligatory method form.

return parents
  .SelectMany(p => p.Children);

And for two levels:

return oldies
  .SelectMany(grand => grand.Children)
  .SelectMany(parent => parent.Children);


This will work:

public IEnumerable<Child> GetAllChildren(IEnumerable<Parent> parents)
    return from parent in parents
           from child in parent.Children
           select child;

and then this:

public IEnumerable<Child> GetAllChildren(IEnumerable<Grandparent> nanas)
    return from papa in nanas
           from parent in papa.Children
           from child in parent.Children
           select child;

Note, in this example I'm not actually returning a list, I'm returning an IEnumerable data source that until you start to foreach over it, or similar, won't actually do any processing.

If you need to return a list, modify each return statement as follows:

    return (from .....
            select child).ToList();

Related Articles