tags:

views:

569

answers:

2

Seems like this is the kind of thing that would have already been answered but I'm unable to find it.

My question is pretty simple, how can I do this in one statement so that instead of having to new the empty list and then aggregate in the next line, that I can have a single linq statement that outputs my final list. details is a list of items that each contain a list of residences, I just want all of the residences in a flat list.

var residences = new List<DAL.AppForm_Residences>();
details.Select(d => d.AppForm_Residences).ToList().ForEach(d => residences.AddRange(d));
+5  A: 

Use SelectMany

var all = residences.SelectMany(x => x.AppForm_Residences);
JaredPar
+5  A: 

You want to use the SelectMany extension method.

var residences = details.SelectMany(d => d.AppForm_Residences).ToList();
Noldorin
Thanks. @JaredPar has the selection from the wrong element, but thank you both for your guidance.
jarrett