加入两个列表一起 [英] Joining two lists together

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

问题描述

如果我有字符串类型(或任何其他类型)的两份名单,什么是连接两个列表的快捷方式?

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);

的AddRange MSDN页面$ C>

这preserves列表的顺序,但它不会删除该<$c$c>Union会怎么做。

This preserves the order of the lists, but it doesn't remove any duplicates which Union would do.

这不会更改列表 A 。如果你想preserve原来的名单,那么你应该使用<一个href=\"https://msdn.microsoft.com/en-us/library/vstudio/bb302894(v=vs.100).aspx\"><$c$c>Concat (如在其他的答案中指出):

This does change list 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。

This returns an IEnumerable as long as a is not null.

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

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