Click here to Skip to main content
11,924,825 members (55,396 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked

GroupBy Multiple Values in LINQ

, 3 Jun 2009
Rate this:
Please Sign up or sign in to vote.
Here's a simple example to show you how to GroupBy Multiple Valuesusing LINQ. In this example, I am grouping by Age and Sex to find thecount of people who have the same age and sex.C#public partial class LINQ : System.Web.UI.Page{    protected void Page_Load(object sender, EventAr

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.


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



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


                Return privateID

            End Get

            Set(ByVal value As Integer)

                privateID = value

            End Set

        End Property

        Private privateFName As String

        Public Property FName() As String


                Return privateFName

            End Get

            Set(ByVal value As String)

                privateFName = value

            End Set

        End Property


        Private privateAge As Integer

        Public Property Age() As Integer


                Return privateAge

            End Get

            Set(ByVal value As Integer)

                privateAge = value

            End Set

        End Property


        Private privateSex As Char

        Public Property Sex() As Char


                Return privateSex

            End Get

            Set(ByVal value As Char)

                privateSex = value

            End Set

        End Property

    End Class

End Class



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

Software Developer (Senior)
United States United States
Winner - Best Mobile App - AT&T Developer Summit, Las Vegas, 2013

My personal resume can be found at:

My game portfolio can be found at:

About Philip Pierce:

I am a software developer with twenty years experience in game development, mobile, web, desktop, server, and database. My extensive background highlights an expertise in rapid application development using the latest Microsoft, Mobile, and Game Development technologies, along with the ability to create AI for games and business software, redesign existing software, develop multi-threaded software, and create client/server applications.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Michael Bertelsen8-Jun-09 12:26
memberMichael Bertelsen8-Jun-09 12:26 
GeneralMy vote of 1 Pin
Jon Artus3-Jun-09 3:47
memberJon Artus3-Jun-09 3:47 
GeneralMy vote of 1 Pin
Josh Fischer3-Jun-09 3:26
memberJosh Fischer3-Jun-09 3:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151125.3 | Last Updated 3 Jun 2009
Article Copyright 2009 by merlin981
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid