C# Linq Intersect 方法

csharpprogrammingserver side programming更新于 2025/9/14 17:52:00

使用 Intersect() 方法查找两个数组之间的共同元素。

以下是我们的数组 −

int[] val1 = { 15, 20, 40, 60, 75, 90 };
int[] val2 = { 17, 25, 35, 55, 75, 90 };

执行交集。

val1.AsQueryable().Intersect(val2);

让我们看看整个示例。

示例

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("两个列表的交集...");
      foreach (int a in res)
      Console.WriteLine(a);
   }
}

输出

两个列表的交集...
75
90

相关文章