C# Linq Distinct() Method

ProgrammingServer Side ProgrammingCsharp

To get the distinct elements, use the Distinct() method.

The following is our list with duplicate elements.

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

Now to get the distinct elements −

points.AsQueryable().Distinct();

Let us see the entire example −

Example

 Live Demo

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);
      }
   }
}

Output

5
10
20
30
40
50
60
70
raja
Published on 11-Sep-2018 12:54:01
Advertisements