最简单的,形成两个列表一个结合的方式 [英] Simplest way to form a union of two lists

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

问题描述

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

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

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

解决方案

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

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

listA.AddRange(数组listB); //现在listA的有数组listB也元素。
 

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

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

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

  VAR数组listB =新的名单,其中,INT&GT; {3,4,5};
  变种listA的=新列表与所述; INT&GT; {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);

这篇关于最简单的,形成两个列表一个结合的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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