加入两个列表一起 [英] Joining two lists together
问题描述
如果我有字符串类型(或任何其他类型)的两份名单,什么是连接两个列表的快捷方式?
If I have two lists of type string (or any other type), what is a quick way of joining the two lists?
顺序应保持不变。重复应该被删除(虽然在这两个环节的每一项都是唯一的)。 google搜索时,并没有想实现任何.NET接口的交付速度,我没有找到太多这一点。
The order should stay the same. Duplicates should be removed (though every item in both links are unique). I didn't find much on this when googling and didn't want to implement any .NET interfaces for speed of delivery.
推荐答案
您可以尝试:
List<string> a = new List<string>();
List<string> b = new List<string>();
a.AddRange(b);
为 这preserves列表的顺序,但它不会删除该<$c$c>Union$c$c>会怎么做。 This preserves the order of the lists, but it doesn't remove any duplicates which 这不会更改列表 This does change list 这将返回 This returns an 这篇关于加入两个列表一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!的AddRange $ C
Union
would do. A
。如果你想preserve原来的名单,那么你应该使用<一个href=\"https://msdn.microsoft.com/en-us/library/vstudio/bb302894(v=vs.100).aspx\"><$c$c>Concat$c$c> (如在其他的答案中指出):a
. If you wanted to preserve the original lists then you should use Concat
(as pointed out in the other answers):var newList = a.Concat(b);
的IEnumerable
只要 A
不为null。IEnumerable
as long as a
is not null.