Union方法从两个列表中获取唯一元素。
让我们设置两个列表-
var list1 = new List<int>{12, 65, 88, 45};
var list2 = new List<int>{40, 34, 65};现在获取两个列表的并集-
var res = list1.Union(list2);
以下是示例-
using System.Collections.Generic;
using System.Linq;
using System;
public class Demo {
public static void Main() {
//两个列表
var list1 = new List<int>{12, 65, 88, 45};
var list2 = new List<int>{40, 34, 65};
//寻找工会
var res = list1.Union(list2);
foreach(int i in res) {
Console.WriteLine(i);
}
}
}输出结果
12 65 88 45 40 34