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




Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

No comments: