How to Remove Duplicates and Get Distinct records from List using LINQ?

Published on August 9, 2013 by abundantcode

Filed under LINQ

Last modified August 9, 2013

Print this page

rate 1 star rate 2 star rate 3 star rate 4 star rate 5 star
Your rating: none, Average: 2.5 (2 votes)

This article have been viewed 27278 times

Below is a sample source code demonstrating on how to remove duplicates and get distinct records in List using LINQ and C#?

How to Remove Duplicates and Get Distinct records from List using LINQ?

using System;

using System.Collections.Generic;

using System.Data;

using System.Linq;

namespace AbundantCode

{

internal class Program

{

//How to Remove Duplicates and Get Distinct records from List using LINQ ?

private static void Main(string[] args)

{

List<Employee> employees = new List<Employee>()

{

new Employee { EmpID = 1 , Name ="AC"},

new Employee { EmpID = 2 , Name ="Peter"},

new Employee { EmpID = 3 , Name ="Michael"},

new Employee { EmpID = 3 , Name ="Michael"}

};

//Gets the Distinct List

var DistinctItems = employees.GroupBy(x => x.EmpID).Select(y => y.First());

foreach(var item in DistinctItems)

Console.WriteLine(item.Name);

Console.ReadLine();

}

}

public class Employee

{

public string Name { get; set; }

public int EmpID { get; set; }

}

}

9 Comments

  1. Rick

    What if you want to find uniqueness over both name and id? As an examaple

    List employees = new List()
    {
    new Employee { EmpID = 1 , Name =”AC”},
    new Employee { EmpID = 2 , Name =”Peter”},
    new Employee { EmpID = 3 , Name =”Michael”},
    new Employee { EmpID = 3 , Name =”Michael”}
    new Employee { EmpID = 3 , Name =”Michael”}
    new Employee { EmpID = 4 , Name =”Michael”}
    };

    results would be:
    ID, Name
    1, AC
    2, Peter
    3, Michael
    4, Michael

    December 27, 2013 - 6:57 pm – Reply

    • AbundantCode

      The existing code takes care of it … am assuming that
      1, AC
      2, Peter
      3, Michael
      4, Michael

      In the result , 3 – Michael and 4- Michael is definitely unique . Are you referring to the uniqueness of name Or id ?

      December 27, 2013 - 7:13 pm – Reply

      • Rick

        I would like to determine the uniqueness of by both name AND id. In your code example
        var DistinctItems = employees.GroupBy(x => x.EmpID).Select(y => y.First());
        the uniqueness is for EmpID only. How to modify this to include the Name?

        February 27, 2014 - 3:48 am – Reply

        • Frebben

          var DistinctItems = employees.GroupBy(x => new { x.EmpID, x.Name }).Select(y => y.First());

          November 11, 2015 - 9:30 pm – Reply

  2. quangnd

    Thanks a lot. It worked fine for me 🙂

    February 25, 2014 - 1:18 pm – Reply

  3. Manish

    Thanks its working 🙂

    June 12, 2015 - 6:49 pm – Reply

  4. ImHungry

    Why do you need the “.Select(y => y.First())? at the end of line 39?

    January 20, 2016 - 2:12 am – Reply

  5. sameer

    help for all column distinct from database

    April 27, 2016 - 3:05 pm – Reply

  6. creiglee

    List duplicates = lst.GroupBy(x => x) .Where(g => g.Count() > 1) .Select(g => g.Key) .ToList();

    Lee

    October 7, 2016 - 12:39 pm – Reply

Leave a Comment


x

You might be Interested in these Posts

How to download a file from a URL using C#?

You can use the WebClient class in C# to download a file. How to download a file from a URL using C...

How to Create an Empty array without defining the size in C# ?

When we create an array in C# , we tend to define the size of the array as shown below. String[] st...

C++ Program to display "Hello, World!"

Problem Write a program in C++ to display "Hello, World!" on the screen. C++ Program to...