在 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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP