如何枚举集合追加到在C#中的现有列表 [英] How to append enumerable collection to an existing list in C#

查看:433
本文介绍了如何枚举集合追加到在C#中的现有列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在返回IEnumerable集合三种功能。
现在我想所有这些组合成一个列表。
这样,有没有通过,我可以添加自IEnumerable项目清单的任何方法。
I意味着无需对每个循环?

i have three functions which are returning an IEnumerable collection. now i want to combine all these into one List. so, is there any method by which i can append items from IEnumerable to a list. i mean without for each loop?

推荐答案

好了,的东西的则要循环。 ..但在LINQ您可以轻松地使用 的毗连 了ToList 扩展程序方法:

Well, something will have to loop... but in LINQ you could easily use the Concat and ToList extension methods:

var bigList = list1.Concat(list2).Concat(list3).ToList();

请注意,这将创建一个新的列表中,而不是追加项目的的现有列表中。如果你想将它们添加到现有列表, 列表< T> .AddRange 可能是你追求的:

Note that this will create a new list rather than appending items to an existing list. If you want to add them to an existing list, List<T>.AddRange is probably what you're after:

bigList.AddRange(list1);
bigList.AddRange(list2);
bigList.AddRange(list3);

这篇关于如何枚举集合追加到在C#中的现有列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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