最简单的,形成两个列表一个结合的方式最简单、两个、方式、列表

2023-09-03 00:43:44 作者:Enchanting 妩媚

什么是比较两个列表元素的最简单的方法说A和B彼此,并添加这是present在B到A的元素,只有当他们没有present于A

要说明这一点, 以列表A = {1,2,3} 列表B = {3,4,5}

所以我想手术后AUB 列表A = {1,2,3,4,5}

解决方案

如果它是一个列表,你也可以使用的的AddRange 方法。

  VAR数组listB =新的名单,其中,INT> {3,4,5};
变种listA的=新列表与所述; INT> {1,2,3,4,5};

listA.AddRange(数组listB); //现在listA的有数组listB也元素。
 
qq想把两个分组的成员合在一起,有没有简便的方法

如果你需要新的列表(和排除重复的),您可以使用工会

  VAR数组listB =新的名单,其中,INT> {3,4,5};
  变种listA的=新列表与所述; INT> {1,2,3,4,5};
  VAR listFinal = listA.Union(数组listB);
 

如果你需要新的列表(包括重复的),您可以使用 CONCAT

  VAR数组listB =新的名单,其中,INT> {3,4,5};
  变种listA的=新列表与所述; INT> {1,2,3,4,5};
  VAR listFinal = listA.Concat(数组listB);
 

What is the easiest way to compare the elements of two lists say A and B with one another, and add the elements which are present in B to A only if they are not present in A?

To illustrate, Take list A = {1,2,3} list B = {3,4,5}

So after the operation AUB I want list A = {1,2,3,4,5}

解决方案

If it is a list, you can also use AddRange method.

var listB = new List<int>{3, 4, 5};  
var listA = new List<int>{1, 2, 3, 4, 5};

listA.AddRange(listB); // listA now has elements of listB also.

If you need new list (and exclude the duplicate), you can use union

  var listB = new List<int>{3, 4, 5};  
  var listA = new List<int>{1, 2, 3, 4, 5};
  var listFinal = listA.Union(listB);

If you need new list (and include the duplicate), you can use concat

  var listB = new List<int>{3, 4, 5};  
  var listA = new List<int>{1, 2, 3, 4, 5};
  var listFinal = listA.Concat(listB);