LINQ to Objects问题 [英] LINQ to Objects question
问题描述
我正在编写一种方法,该方法将传递给List< AssetMovements> AssetMovements看起来像
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; }
}
我希望能够将这些对象展平到所有Movements的列表中,而与Description无关,并且试图找出LINQ查询,我需要这样做.我以为
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
将执行此操作并返回IEnumerable< DateRange>.但相反,它返回IEnumerable< List< List< DateRange>>.而且我不太确定如何解决这个问题.有什么建议吗?
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?
推荐答案
已经有人问过这个了.您需要使用SelectMany()方法,该方法可以使列表列表平坦化.所以:
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);
这篇关于LINQ to Objects问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!