Click here to Skip to main content
11,706,799 members (45,228 online)
Click here to Skip to main content

Tagged as

How to Use LINQ to Get a Count of Duplicates in a List

, 2 Jun 2009 21.6K 2
Rate this:
Please Sign up or sign in to vote.
Often we have to get a count of duplicate items in a list. An easy way to do this is to group the list on the property we want to count, then use LINQ&#39;s GroupBy&#39;s Count feature. Below is a quick example using an employee list.  private static void Main() {     List<Employee> empLi

Often we have to get a count of duplicate items in a list. An easy way to do this is to group the list on the property we want to count, then use LINQ's GroupBy's Count feature. Below is a quick example using an employee list.

 

private static void Main()
{
    List<Employee> empList = new List<Employee>();

    empList.Add(new Employee() { ID = 1, FName = "John", Age=23, Sex='M'  });
    empList.Add(new Employee() { ID = 2, FName = "Mary", Age = 25, Sex = 'F' });
    empList.Add(new Employee() { ID = 3, FName = "Amber", Age = 23, Sex = 'M' });
    empList.Add(new Employee() { ID = 4, FName = "Kathy", Age=25, Sex='M'});
    empList.Add(new Employee() { ID = 5, FName = "Lena", Age=27, Sex='F'});
    empList.Add(new Employee() { ID = 6, FName = "John", Age = 28, Sex = 'M' });
    empList.Add(new Employee() { ID = 7, FName = "Kathy", Age = 27, Sex = 'F' });
    empList.Add(new Employee() { ID = 8, FName = "John", Age = 28, Sex = 'M' });

    var dup = empList
        .GroupBy(x => new { x.FName })
        .Select(group => new { Name = group.Key, Count = group.Count() })
        .OrderByDescending(x => x.Count);

    foreach (var x in dup)
    {
        Response.Write(x.Count + " " + x.Name);
    }
}


class Employee
{
    public int ID { get; set; }
    public string FName { get; set; }
    public int Age { get; set; }
    public char Sex { get; set; }
}


License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

merlin981
Software Developer (Senior)
United States United States
Winner - Best Mobile App - AT&T Developer Summit, Las Vegas, 2013

My personal resume can be found at: http://www.philippiercedeveloper.com

My game portfolio can be found at: http://www.rocketgamesmobile.com

About Philip Pierce:

I am a software developer with twenty years experience in game development, mobile, web, desktop, server, and database. My extensive background highlights an expertise in rapid application development using the latest Microsoft, Mobile, and Game Development technologies, along with the ability to create AI for games and business software, redesign existing software, develop multi-threaded software, and create client/server applications.

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 1 Pin
Jon Artus3-Jun-09 2:52
memberJon Artus3-Jun-09 2:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150819.1 | Last Updated 3 Jun 2009
Article Copyright 2009 by merlin981
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid