jQuery and ASP.NET

February 12, 2009

GroupBy Multiple Values in LINQ




Here's a simple example to show you how to GroupBy Multiple Values using LINQ. In this example, I am grouping by Age and Sex to find the count of people who have the same age and sex

C#


public partial class LINQ : System.Web.UI.Page


{


    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 = "Bill", Age = 28, Sex = 'M' });


    empList.Add(new Employee() { ID = 7, FName = "Celina", Age = 27, Sex = 'F' });


    empList.Add(new Employee() { ID = 8, FName = "John", Age = 28, Sex = 'M' });


 


 


    var sums = empList


               .GroupBy(x => new { x.Age, x.Sex })


               .Select(group => new { Peo = group.Key, Count = group.Count() });


 


    foreach (var employee in sums)


    {


        Response.Write(employee.Count + ": " + employee.Peo);


    }


 


 


    }


 


    class Employee


    {


        public int ID { get; set; }


        public string FName { get; set; }


        public int Age { get; set; }


        public char Sex { get; set; }


    }




VB.NET


Partial Public Class LINQ


    Inherits System.Web.UI.Page


    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 = "Bill", .Age = 28, .Sex = "M"c})


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


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


 


 


        Dim sums = empList.GroupBy(Function(x) New With {Key x.Age, Key x.Sex}).Select(Function(group) New With {Key .Peo = group.Key, Key .Count = group.Count()})


 


        For Each employee In sums


            ' use employee.Count and employee.Key


        Next employee


 


 


    End Sub


 


 


 


    Public 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



Bookmark this link on del.icio.us (saved by 0 users)

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

comments

2 Responses to "GroupBy Multiple Values in LINQ"
  1. Busca Mexico said...
    December 20, 2009 2:52 PM

    finally an easy example in how to group by two or more columns! Thank you!!

  2. Marin said...
    July 3, 2010 12:12 PM

    Thanks. Best article containing GroupBy in whole internet ;)

 

Copyright 2010 All Rights Reserved DevCurry.com by Suprotim Agarwal