Linq列表列表到单个列表 [英] Linq list of lists to single list

查看:58
本文介绍了Linq列表列表到单个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好像这样的事情本来应该已经回答了,但我找不到.

Seems like this is the kind of thing that would have already been answered but I'm unable to find it.

我的问题很简单,我如何在一个语句中执行此操作,这样我不必使用新的空列表,然后在下一行进行汇总,而只需一个linq语句即可输出最终列表.详细信息是一个项目列表,每个项目都包含一个住所列表,我只想将所有住所都放在一个平面列表中.

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扩展方法.

var residences = details.SelectMany(d => d.AppForm_Residences).ToList();

这篇关于Linq列表列表到单个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆