Click here to Skip to main content
6,822,123 members and growing! (19,197 online)
Email Password   helpLost your password?


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

By merlin981

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 empLi
All-Topics
Posted:3 Jun 2009
Views:2,834
Bookmarked:2 times
Technical Blog
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
1 vote for this technical blog.
Popularity: 0.00 Rating: 1.00 out of 5
1 vote, 100.0%
1

2

3

4

5
A Technical Blog article. View entire blog here.

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

About the Author

merlin981


Member

Occupation: Web Developer
Location: United States United States

Other popular Uncategorised Technical Blogs articles:

  • Delegates Explained in Plain English
    CodeProjectDelegates are fundamental to the .NET Framework (events and callbacks wouldn't work without them) and can be extremely powerful to the .NET Developer once they come to grasps with exactly what they are and how to use them. In this blog I will consider aspects of a real world situation in
  • Multi-Threading in ASP.NET
    ASP.Net Threading Inside the ASP.Net Worker Process there are two thread pools. Theworker thread pool handles all incoming requests and the I/O Threadpool handles the I/O (accessing the file system, web services anddatabases, etc.). Each App Domain has its own thread pool and thenumber of ope
  • Windows 7 Tricks and Keyboard Shortcuts
    I’ve been running Windows 7 RC for a little over a week now and can’t imagine going back to Vista at this point. I decided to start with a fresh install of Windows 7, so I’ve been in the process of reinstalling all of my applications and cleaning up my disk drives. In the process, I went searching
  • iPhone Gaming Framework: Stage 1 Tutorial
    The goal for this tutorial is to get a basic screen management system up and running, ready to start writing game code.
  • Thread Safe Generic Queue Class
    I've been doing a lot of mult-threading work, recently, using the standard Thead class, the Worker Queue, and the new PLINQ (Parallel LINQ). The problem with most of the built-in generic collections (Queue, List, Dictionary, etc), is that they are not thread safe.I created a library of
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
GeneralMy vote of 1 PinmemberJon Artus3:52 3 Jun '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 3 Jun 2009
Editor:
Copyright 2009 by merlin981
Everything else Copyright © CodeProject, 1999-2010
Web21 | Advertise on the Code Project