与C#中的两个列表相交
首先,设置两个列表。
List<int> val1 = new List<int> { 25, 30, 40, 60, 80, 95, 110 };
List<int> val2 = new List<int> { 27, 35, 40, 75, 95, 100, 110 };现在,使用该Intersect()方法获取两个列表之间的交集。
IEnumerable<int> res = val1.AsQueryable().Intersect(val2);
示例
using System;
using System.Collections.Generic;
using System.Linq;
class Demo {
static void Main() {
List<int> val1 = new List<int> { 25, 30, 40, 60, 80, 95, 110 };
List<int> val2 = new List<int> { 27, 35, 40, 75, 95, 100, 110 };
IEnumerable<int> res = val1.AsQueryable().Intersect(val2);
Console.WriteLine("Intersection of both the lists...");
foreach (int a in res) {
Console.WriteLine(a);
}
}
}输出结果
Intersection of both the lists... 40 95 110
热门推荐
10 朋友弟弟生日祝福语简短
11 高考祝福语 简短12字
12 生日祝福语简短激励女生
13 祝贺新店开业祝福语简短
14 送礼祝福语男性生日简短
15 学生祝福语简短古诗句
16 租房明天搬家祝福语简短
17 虎年感慨祝福语简短英文
18 双十一祝福语大全简短