如果我有两个字符串类型(或任何其他类型)的列表,加入这两个列表的快捷方式是什么?将两个列表连接在一起
订单应该保持不变。应删除重复项目(尽管两个链接中的每个项目都是唯一的)。在Google上搜索时,我没有发现太多内容,也不希望为了交付速度而实现任何.NET接口。
如果我有两个字符串类型(或任何其他类型)的列表,加入这两个列表的快捷方式是什么?将两个列表连接在一起
订单应该保持不变。应删除重复项目(尽管两个链接中的每个项目都是唯一的)。在Google上搜索时,我没有发现太多内容,也不希望为了交付速度而实现任何.NET接口。
你可以尝试:
List<string> a = new List<string>();
List<string> b = new List<string>();
a.AddRange(b);
这将保留该列表的顺序,但不会删除任何重复这Union
会做。
确实更改列表a
。如果你想保留原来的名单,那么你应该使用Concat
(如在其他的答案中指出):
var newList = a.Concat(b);
这只要a
不是null返回IEnumerable
。
没有人真正了解何时使用哪种方法。 AddRange编辑一个列表,添加第二个列表(就像你多次调用.Add(foo)一样)。 Concat和Union扩展方法不会更改原始列表。他们懒惰地构造一个新的IEnumerable,除非必要,甚至不会访问原始列表成员。如上所述,联盟删除重复,而其他人不重复。 – ShawnFumo 2015-01-02 02:15:34
当其中一个列表为空时,'concat'不起作用 – Amit 2015-12-18 20:10:27
有谁知道这种方法的复杂性是什么? (很遗憾,微软不提供这些重要信息作为他们的MSDN的一部分) – Jacob 2016-11-14 18:00:26
事情是这样的:
firstList.AddRange (secondList);
或者,您可以使用在System.Linq的定义的“联盟”的扩展方法。 使用'联合',你也可以指定一个比较器,它可以用来指定一个项目是否应该联合。
像这样:
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
Union方法可能会满足您的需求。您没有指定订单或重复是否重要。这取决于类型List.AddRange():
吃两个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 }
的一种方式?
aList.AddRange(anotherList);
用最少的空间开销的方法是使用所述的毗连扩展方法。
var combined = list1.Concat(list2);
它创建的IEnumerable<T>
它的实例将枚举list1的和list2中的元素的顺序。
只要它们是同一类型的,这是非常简单的用的AddRange:
list2.AddRange(list1);
var bigList = new List<int> { 1, 2, 3 }
.Concat(new List<int> { 4, 5, 6 })
.ToList(); /// yields { 1, 2, 3, 4, 5, 6 }
我也喜欢。很简单。谢谢。 – dotnetdev 2009-10-06 22:22:35
我喜欢这个,因为它不会改变任何一个列表。 – 2015-01-13 02:02:07
如果某些项目(S)这两个列表中存在,您可以使用
var all = list1.Concat(list2).Concat(list3) ... Concat(listN).Distinct().ToList();
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);
targetList = list1.Concat(list2).ToList();
它工作正常我认为是。如前所述,Concat返回一个新序列,并将结果转换为List,它完美地完成了这项工作。在使用AddRange方法时,隐式转换可能会失败。
看到这个link
public class ProductA
{
public string Name { get; set; }
public int Code { get; set; }
}
public class ProductComparer : IEqualityComparer<ProductA>
{
public bool Equals(ProductA x, ProductA y)
{
//Check whether the objects are the same object.
if (Object.ReferenceEquals(x, y)) return true;
//Check whether the products' properties are equal.
return x != null && y != null && x.Code.Equals(y.Code) && x.Name.Equals(y.Name);
}
public int GetHashCode(ProductA obj)
{
//Get hash code for the Name field if it is not null.
int hashProductName = obj.Name == null ? 0 : obj.Name.GetHashCode();
//Get hash code for the Code field.
int hashProductCode = obj.Code.GetHashCode();
//Calculate the hash code for the product.
return hashProductName^hashProductCode;
}
}
ProductA[] store1 = { new ProductA { Name = "apple", Code = 9 },
new ProductA { Name = "orange", Code = 4 } };
ProductA[] store2 = { new ProductA { Name = "apple", Code = 9 },
new ProductA { Name = "lemon", Code = 12 } };
//获取产品从两个阵列 //不重复计算。
IEnumerable<ProductA> union =
store1.Union(store2);
foreach (var product in union)
Console.WriteLine(product.Name + " " + product.Code);
/*
This code produces the following output:
apple 9
orange 4
lemon 12
*/
我只是想测试如何Union
作品与重叠的引用类型对象的集合默认的比较。
我的目标是:
class MyInt
{
public int val;
public override string ToString()
{
return val.ToString();
}
}
我的测试代码:
MyInt[] myInts1 = new MyInt[10];
MyInt[] myInts2 = new MyInt[10];
int overlapFrom = 4;
Console.WriteLine("overlapFrom: {0}", overlapFrom);
Action<IEnumerable<MyInt>, string> printMyInts = (myInts, myIntsName) => Console.WriteLine("{2} ({0}): {1}", myInts.Count(), string.Join(" ", myInts), myIntsName);
for (int i = 0; i < myInts1.Length; i++)
myInts1[i] = new MyInt { val = i };
printMyInts(myInts1, nameof(myInts1));
int j = 0;
for (; j + overlapFrom < myInts1.Length; j++)
myInts2[j] = myInts1[j + overlapFrom];
for (; j < myInts2.Length; j++)
myInts2[j] = new MyInt { val = j + overlapFrom };
printMyInts(myInts2, nameof(myInts2));
IEnumerable<MyInt> myUnion = myInts1.Union(myInts2);
printMyInts(myUnion, nameof(myUnion));
for (int i = 0; i < myInts2.Length; i++)
myInts2[i].val += 10;
printMyInts(myInts2, nameof(myInts2));
printMyInts(myUnion, nameof(myUnion));
for (int i = 0; i < myInts1.Length; i++)
myInts1[i].val = i;
printMyInts(myInts1, nameof(myInts1));
printMyInts(myUnion, nameof(myUnion));
输出是:
overlapFrom: 4
myInts1 (10): 0 1 2 3 4 5 6 7 8 9
myInts2 (10): 4 5 6 7 8 9 10 11 12 13
myUnion (14): 0 1 2 3 4 5 6 7 8 9 10 11 12 13
myInts2 (10): 14 15 16 17 18 19 20 21 22 23
myUnion (14): 0 1 2 3 14 15 16 17 18 19 20 21 22 23
myInts1 (10): 0 1 2 3 4 5 6 7 8 9
myUnion (14): 0 1 2 3 4 5 6 7 8 9 20 21 22 23
所以,一切工作正常。
命令是否重要?你想保留重复吗? – Larsenal 2009-10-06 21:28:16
* [您如何连接C#中的列表?](http://stackoverflow.com/questions/1042219/how-do-you-concatenate-lists-in-c)*。 – 2015-11-22 03:12:38