首先,创建列表-
//三个列表 var list1 = new List{3, 4, 5}; var list2 = new List{1, 2, 3, 4, 5}; var list3 = new List{5, 6, 7, 8};
使用union方法获取list1和list2的并集-
var res1 = list1.Union(list2); var res2 = res1.Union(list3);
以下是完整的代码-
using System.Collections.Generic; using System.Linq; using System; public class Demo { public static void Main() { //三个列表 var list1 = new List<int>{3, 4, 5}; var list2 = new List<int>{1, 2, 3, 4, 5}; var list3 = new List<int>{5, 6, 7, 8}; //寻找工会 var res1 = list1.Union(list2); var res2 = res1.Union(list3); foreach(int i in res2) { Console.WriteLine(i); } } }
输出结果
3 4 5 1 2 6 7 8