Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to Objects question

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?

like image 885
Steve Crane Avatar asked Mar 06 '26 09:03

Steve Crane


1 Answers

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);
like image 104
Matt Hamilton Avatar answered Mar 10 '26 00:03

Matt Hamilton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!