Tìm các phần tử chung giữa hai mảng bằng phương thức Intersect ().
Sau đây là các mảng của chúng tôi -
int[] val1 = { 15, 20, 40, 60, 75, 90 };
int[] val2 = { 17, 25, 35, 55, 75, 90 }; Để thực hiện giao cắt.
val1.AsQueryable().Intersect(val2);
Hãy để chúng tôi xem toàn bộ ví dụ.
Ví dụ
using System;
using System.Collections.Generic;
using System.Linq;
class Demo {
static void Main() {
int[] val1 = { 15, 20, 40, 60, 75, 90 };
int[] val2 = { 17, 25, 35, 55, 75, 90 };
IEnumerable<int> res = val1.AsQueryable().Intersect(val2);
Console.WriteLine("Intersection of both the lists...");
foreach (int a in res)
Console.WriteLine(a);
}
} Đầu ra
Intersection of both the lists... 75 90