C# Linq Distinct() 方法


要獲取不同元素,請使用 Distinct() 方法。

下面是有重複元素的列表。

List<int> points = new List<int> { 5, 10, 5, 20, 30, 30, 40, 50, 60, 70 };

現在獲取不同元素 -

points.AsQueryable().Distinct();

讓我們看看整個例子 -

示例

 現場演示

using System;
using System.Linq;
using System.Collections.Generic;
class Demo {
   static void Main() {
      List<int> points = new List<int> { 5, 10, 5, 20, 30, 30, 40, 50, 60, 70 };
      // distict elements from the list
      IEnumerable<int> res = points.AsQueryable().Distinct();
      foreach (int a in res) {
         Console.WriteLine(a);
      }
   }
}

輸出

5
10
20
30
40
50
60
70

更新時間:2020 年 6 月 22 日

652 次瀏覽

開啟你的 職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.