Objective

In this article, I am going to show, how we could achieve grouping in LINQ to object

I have created two classes for my explanation purpose. Student class is having details of students and Hostel class is having details of hostel. Both classes are having a properties HostelNumber common. I will be using this property to perform join operations in later articles. Both classes are as below.

Student.cs

    1 using System;

    2 using System.Collections.Generic;

    3 using System.Linq;

    4 using System.Text;

    5 

    6 namespace LINQtoOBJECT1

    7 {

    8      public  class Student

    9     {

   10 

   11          public int RollNumber { get; set; }

   12          public string Name { get; set; }

   13          public int Section { get; set; }

   14          public int HostelNumber { get; set; }

   15     }

   16 }

   17 

   Hostel.cs                    

    3 using System.Linq;

    4 using System.Text;

    5 

    6 namespace LINQtoOBJECT1

    7 {

    8    public class Hostel

    9     {

   10 

   11        public int HostelNumber { get; set; }

   12        public int NumberofRooms { get; set; }

   13     }

   14 }

   15 


To create collection of students and hostels, I am creating two static functions.

Function to return collection of students.

    1    static List<Student> GetStudents()

    2         {

    3             List<Student> students = new List<Student>

    4             {

    5                 new Student() { RollNumber = 1,Name =“Alex “ , Section =  1 ,HostelNumber=1 },

    6                 new Student() { RollNumber = 2,Name =“Jonty “ , Section = 2 ,HostelNumber=2 },

    7                 new Student() { RollNumber = 3,Name =“Samba “ , Section = 3 ,HostelNumber=1 },

    8                 new Student() { RollNumber = 4,Name =“Donald “ , Section = 3 ,HostelNumber=2 },

    9                 new Student() { RollNumber = 5,Name =“Kristen “ , Section = 2 ,HostelNumber=1 },

   10                 new Student() { RollNumber = 6,Name =“Mark “ , Section = 1 ,HostelNumber=2},

   11                 new Student() { RollNumber = 7,Name =“Gibbs “ , Section = 1 ,HostelNumber=1 },

   12                 new Student() { RollNumber = 8,Name =“Peterson “ , Section = 2 ,HostelNumber=2 },

   13                 new Student() { RollNumber = 9,Name =“collingwood “ , Section = 3 ,HostelNumber=1 },

   14                 new Student() { RollNumber = 10,Name =“Brian “ , Section = 3 ,HostelNumber=2 }

   15 

   16             };

   17 

   18             return students;

   19 

   20         }

 Function to return collection of hostels

    1  static List<Hostel> GetHostel()

    2         {

    3             List<Hostel> hostels = new List<Hostel>

    4             {

    5 

    6                 new Hostel(){HostelNumber=1 ,NumberofRooms = 100},

    7                 new Hostel(){HostelNumber= 2 ,NumberofRooms = 200}

    8             };

    9             return hostels;

   10         }


Now have a look on both codes below, one is using LINQ and other is using LOOP to retrieve data from the list and print.
Note: I will be using above two classes and functions for my entire sample below.
Grouping in LINQ


 Syntax of Groupby

Var result = FROM selectVariable IN dataSourceorList GROUPBY selectVariable.variable INTO RANGEVARIABLE Select new { };

Grouping by a single property

    1  class Program

    2     {

    3         static void Main(string[] args)

    4         {

    5 

    6 

    7           List<Student> lstStudents = GetStudents();

    8           var  lstStudentName = from r in lstStudents

    9                                                    group r by r.HostelNumber into rngStudent

   10                                                    select new

   11                                                    {

   12                                                        HostelID = rngStudent.Key,

   13                                                        Students = rngStudent

   14                                                    };

   15 

   16           foreach (var name in lstStudentName)

   17           {

   18               Console.WriteLine(name.HostelID);

   19               foreach (Student s in name.Students)

   20               {

   21                   Console.WriteLine(s.RollNumber + s.Name);

   22               }

   23           }

   24              Console.ReadKey();

   25 

   26         }

   27 

 

 

 Explanation

  1. I am grouping on property hostel number.
  2. rngStudent is range variable in which ; I am putting the intermediate result.
  3. I am selecting the result in as anonymous type.
  4. While printing the result , first I am printing the key of group by and then retrieving the object in foreach.

        1 foreach (var name in lstStudentName)

        2           {

        3               Console.WriteLine(name.HostelID);

        4               foreach (Student s in name.Students)

        5               {

        6                   Console.WriteLine(s.RollNumber + s.Name);

        7               }

        8           }

    Output


 Accessing the Group object

    1 foreach (var name in lstStudentName)

    2           {

    3               Console.WriteLine(name.HostelID);

    4               foreach (Student s in name.Students)

    5               {

    6                   Console.WriteLine(s.RollNumber + s.Name);

    7               }

    8           }

If you see the above loop , first I am looping the key for the group by clause and then , I am looping through IEnumerable LINQ object. This object is containing the real result.

Conclusion

In this article, I have discussed GROUPING in LINQ to OBJECT. Thanks for reading.

Advertisements

One thought on “LINQ to Object Part #3: Grouping

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s