List Count of Duplicate Names in LINQ

If you have a Generic list with Employee Information and want to take a count of the Employees having the same name, here's how to do so:

C#

    protected void Page_Load(object sender, EventArgs e)


    {


    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; }


    }




VB.NET


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)


        Dim empList As New List(Of Employee)()


        empList.Add(New Employee() With {.ID = 1, .FName = "John", .Age = 23, .Sex = "M"c})


        empList.Add(New Employee() With {.ID = 2, .FName = "Mary", .Age = 25, .Sex = "F"c})


        empList.Add(New Employee() With {.ID = 3, .FName = "Amber", .Age = 23, .Sex = "M"c})


        empList.Add(New Employee() With {.ID = 4, .FName = "Kathy", .Age = 25, .Sex = "M"c})


        empList.Add(New Employee() With {.ID = 5, .FName = "Lena", .Age = 27, .Sex = "F"c})


        empList.Add(New Employee() With {.ID = 6, .FName = "John", .Age = 28, .Sex = "M"c})


        empList.Add(New Employee() With {.ID = 7, .FName = "Kathy", .Age = 27, .Sex = "F"c})


        empList.Add(New Employee() With {.ID = 8, .FName = "John", .Age = 28, .Sex = "M"c})


 


        Dim dup = empList _


        .GroupBy(Function(x) New With {Key x.FName}) _


        .Select(Function(group) New With {Key .Name = group.Key, Key .Count = group.Count()}) _


        .OrderByDescending(Function(x) x.Count)


        For Each x In dup


            ' Use x.count and x.Name


        Next x


    End Sub


 


    Friend Class Employee


        Private privateID As Integer


        Public Property ID() As Integer


            Get


                Return privateID


            End Get


            Set(ByVal value As Integer)


                privateID = value


            End Set


        End Property


        Private privateFName As String


        Public Property FName() As String


            Get


                Return privateFName


            End Get


            Set(ByVal value As String)


                privateFName = value


            End Set


        End Property


        Private privateAge As Integer


        Public Property Age() As Integer


            Get


                Return privateAge


            End Get


            Set(ByVal value As Integer)


                privateAge = value


            End Set


        End Property


        Private privateSex As Char


        Public Property Sex() As Char


            Get


                Return privateSex


            End Get


            Set(ByVal value As Char)


                privateSex = value


            End Set


        End Property


    End Class


End Class







About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

No comments: