LINQ to Objects question

限于喜欢 提交于 2019-12-24 00:54:06

问题


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?


回答1:


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);


来源:https://stackoverflow.com/questions/780867/linq-to-objects-question

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!