C# Linq Distinct() Method


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

Updated on: 22-Jun-2020

423 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements