0

Question is How do a return a List of B with all the entities in children of all Parents without resorting to the type of code below, I was thinking u must be able to acheive the same within a single linq query?

Class Parent {
    public Title,
    public children List<B>,
}

data = List<A>

var childLists = from x in x.Parents select x.children;             
List<B> output = new List<B>();

foreach (List<B> b in childLists)
    output.AddRange(b);

Thanks.

3 Answers3

4
List<B> allChildren = x.Parents.SelctMany(p => p.children).ToList()
Lee
  • 142,018
  • 20
  • 234
  • 287
3
var output = x.Parents.SelectMany(p => p.children).ToList();
LukeH
  • 263,068
  • 57
  • 365
  • 409
1

using nesting

from parent in x.Parents 
  from child in parent.Children 
  select child;
Shay Erlichmen
  • 31,691
  • 7
  • 68
  • 87