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 30031 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 decode a base64 string in C#?

Here's a sample code snippet demonstrating how you can easily decode a base64 encoded string in C#. ...

How to disable editing of items in a combo box in c#?

When working in Win forms using C# and especially when you use the combo box , you would have notice...

C# Program to find the ASCII Value of a Character

Problem Write a program in Visual C# to find the ASCII value of the input character. How to find t...