C# Linq Intersect 方法


使用 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("Intersection of both the lists...");
      foreach (int a in res)
      Console.WriteLine(a);
   }
}

輸出

Intersection of both the lists...
75
90

更新時間: 2020-06-23

481 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告