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 次浏览

开启你的 职业生涯

完成课程即可获得认证

开始
广告