jQuery and ASP.NET

February 14, 2009

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




'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP 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 has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

0 Responses to "List Count of Duplicate Names in LINQ"
 

Copyright © 2009-2011 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions