Linq 列表到单个列表 [英] Linq list of lists to single list
本文介绍了Linq 列表到单个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这似乎是一种已经得到回答但我无法找到的问题.
Seems like this is the kind of thing that would have already been answered but I'm unable to find it.
我的问题非常简单,我如何在一个语句中执行此操作,以便不必新建空列表然后在下一行进行聚合,而是可以使用单个 linq 语句来输出我的最终列表.details 是一个项目列表,每个项目都包含一个住宅列表,我只想要一个平面列表中的所有住宅.
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));
推荐答案
您想使用 SelectMany
扩展方法.
You want to use the SelectMany
extension method.
var residences = details.SelectMany(d => d.AppForm_Residences).ToList();
这篇关于Linq 列表到单个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文