먼저 두 개의 목록을 설정합니다.
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