views:

113

answers:

1

I am writing a method that is passed a List<AssetMovements> where AssetMovements looks something like

public class AssetMovements
{
  public string Description { get; set; }
  public List<DateRange> Movements { get; set; }
}

I want to be able to flatten out these objects into a list of all Movements regardless of Description and am trying to figure out the LINQ query I need to do this. I thought that

from l in list select l.Movements

would do it and return IEnumerable<DateRange> but instead it returns IEnumerable<List<DateRange>> and I'm not really sure how to correct this. Any suggestions?

+11  A: 

This one's been asked before. You want the SelectMany() method, which flattens out a list of lists. So:

var movements = list.SelectMany(l => l.Movements);
Matt Hamilton
Ah brilliant, that was it.
Steve Crane