An enthusiastic, adaptive, and fast-learning person with a broad and acute interest in the discovery of new innovative drugs, I particularly enjoy collaborating with scientists from different disciplines to develop new skills and solve new challenges.
In LINQ, the Distinct method or operator is used to get only distinct elements from the collection or list. In another word these distinct method doesn’t accept the duplicate elements, means remove the duplicate value make its single value.
Syntax
collection1.Distinct ( collection2)
Example
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
List<string> count1= new List<string>() { 'UK', 'Australia', 'India', 'USA', 'India', 'UAE', 'Canada', 'Uk', 'China', 'Pok' };
// ignore the case
//List<string> list = count1.Distinct(StringComparer.OrdinalIgnoreCase).ToList();
List<string> list = count1.Distinct().OrderBy( str => str).ToList();
list.ForEach( country => Console.WriteLine(country) );
Console.ReadLine();
}
}
Output
after using orderby() method result in ascending order
Australia
Canada
China
India
Pok
UAE
Uk
UK
USA
Liked By
Write Answer
HOW to fetch Unique data from LINQ using the Distinct Method?
Join MindStick Community
You have need login or register for voting of answers or question.
Ravi Vishwakarma
28-Sep-2021In LINQ, the Distinct method or operator is used to get only distinct elements from the collection or list. In another word these distinct method doesn’t accept the duplicate elements, means remove the duplicate value make its single value.
Syntax
Example
Output
after using orderby() method result in ascending order