C# 将两个列表连接在一起

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1528171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:27:16  来源:igfitidea点击:

Joining two lists together

c#

提问by GurdeepS

If I have two lists of type string (or any other type), what is a quick way of joining the two lists?

如果我有两个字符串类型(或任何其他类型)的列表,加入这两个列表的快速方法是什么?

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.

顺序应该保持不变。应该删除重复项(尽管两个链接中的每个项目都是唯一的)。我在谷歌搜索时没有找到太多关于这个的东西,也不想实现任何 .NET 接口以提高交付速度。

采纳答案by ChrisF

You could try:

你可以试试:

List<string> a = new List<string>();
List<string> b = new List<string>();

a.AddRange(b);

MSDN page for AddRange

MSDN 页面 AddRange

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

这保留了列表的顺序,但不会删除任何重复项Union

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

这确实改变了 list a。如果您想保留原始列表,那么您应该使用Concat(如其他答案中所指出的):

var newList = a.Concat(b);

This returns an IEnumerableas long as ais not null.

IEnumerable只要a不为空,就返回一个。

回答by Frederik Gheysels

Something like this:

像这样的东西:

firstList.AddRange (secondList);

Or, you can use the 'Union' extension method that is defined in System.Linq. With 'Union', you can also specify a comparer, which can be used to specify whether an item should be unioned or not.

或者,您可以使用 System.Linq 中定义的“Union”扩展方法。使用“联合”,您还可以指定一个比较器,它可用于指定是否应联合某个项目。

Like this:

像这样:

List<int> one = new List<int> { 1, 2, 3, 4, 5 };
List<int> second=new List<int> { 1, 2, 5, 6 };

var result = one.Union (second, new EqComparer ());

foreach( int x in result )
{
    Console.WriteLine (x);
}
Console.ReadLine ();

#region IEqualityComparer<int> Members
public class EqComparer : IEqualityComparer<int>
{
    public bool Equals( int x, int y )
    {
        return x == y;
    }

    public int GetHashCode( int obj )
    {
        return obj.GetHashCode ();
    }
}
#endregion

回答by Larsenal

The Unionmethod might address your needs. You didn't specify whether order or duplicates was important.

联盟的方法可能会满足您的需求。您没有指定顺序或重复是否重要。

Take two IEnumerables and perform a union as seen here:

取两个 IEnumerables 并执行联合,如下所示:

int[] ints1 = { 5, 3, 9, 7, 5, 9, 3, 7 };
int[] ints2 = { 8, 3, 6, 4, 4, 9, 1, 0 };

IEnumerable<int> union = ints1.Union(ints2);

// yields { 5, 3, 9, 7, 8, 6, 4, 1, 0 } 

回答by Mark Redman

one way: List.AddRange() depending on the types?

一种方式: List.AddRange() 取决于类型?

回答by Greg

The AddRange method

的AddRange方法

aList.AddRange( anotherList );

回答by JaredPar

The way with the least space overhead is to use the Concat extension method.

空间开销最小的方式是使用Concat扩展方式。

var combined = list1.Concat(list2);

It creates an instance of IEnumerable<T>which will enumerate the elements of list1 and list2 in that order.

它创建一个实例,IEnumerable<T>该实例将按顺序枚举 list1 和 list2 的元素。

回答by Badaro

As long as they are of the same type, it's very simple with AddRange:

只要它们的类型相同,使用 AddRange 就非常简单:

list2.AddRange(list1);

回答by Dave

var bigList = new List<int> { 1, 2, 3 }
    .Concat(new List<int> { 4, 5, 6 })
    .ToList(); /// yields { 1, 2, 3, 4, 5, 6 }

回答by jimpanzer

If some item(s) exist in both lists you may use

如果两个列表中都存在某些项目,您可以使用

var all = list1.Concat(list2).Concat(list3) ... Concat(listN).Distinct().ToList();

回答by Krishan

List<string> list1 = new List<string>();
list1.Add("dot");
list1.Add("net");

List<string> list2 = new List<string>();
list2.Add("pearls");
list2.Add("!");

var result = list1.Concat(list2);