GroupBy

We use the GroupBy clause in LINQ when we need to group elements based upon some key value. Each group is represented by a respective key and a collection of grouped elements. 

To explain this operator, we will consider the same Class and Student example we have been discussing throughout this chapter. Let's consider a scenario wherein we need to group students based upon the classes they are currently enrolled in. 

To recap, the following is the structure of the Student class:

public class Student
{
public int rollNum { get; set; }
public string Name { get; set; }
public string classID { get; set; }
public int age { get; set; }
}

Let's assume that we have the following objects in the Student class:

List<Student> students = new List<Student>();
students.Add(new Student { rollNum = 1, classID = "1", Name = "Sia Bhalla", age = 1 });
students.Add(new Student { rollNum = 2, classID = "2", Name = "James Donohoe", age = 35 });
students.Add(new Student { rollNum = 3, classID = "1", Name = "Myra Thareja", age = 8 });
students.Add(new Student { rollNum = 4, classID = "3", Name = "Simaranjit Bhalla", age = 33 });
students.Add(new Student { rollNum = 5, classID = "3", Name = "Jimmy Bhalla", age = 33 });
students.Add(new Student { rollNum = 6, classID = "2", Name = "Misha Thareja", age = 35 });

To group the students in terms of class ID, we use the following code:

 List<Student> students = new List<Student>();
students.Add(new Student { rollNum = 1, classID = "1", Name = "Sia Bhalla", age = 1 });
students.Add(new Student { rollNum = 2, classID = "2", Name = "James Donohoe", age = 35 });
students.Add(new Student { rollNum = 3, classID = "1", Name = "Myra Thareja", age = 8 });
students.Add(new Student { rollNum = 4, classID = "3", Name = "Simaranjit Bhalla", age = 33 });
students.Add(new Student { rollNum = 5, classID = "3", Name = "Jimmy Bhalla", age = 33 });
students.Add(new Student { rollNum = 6, classID = "2", Name = "Misha Thareja", age = 35 });
var groupedResult = from s in students
group s by s.classID;
//iterate each group
foreach (var classGroup in groupedResult)
{
Console.WriteLine("Class Group: {0}", classGroup.Key);
foreach (Student s in classGroup)
Console.WriteLine("Student Name: {0}", s.Name);
}

In the preceding code, we have created six objects of student class and are then trying to group them by ClassID. After the grouping is complete, we are looping through the groups that have been created. We are printing Key, which is, in this case, the class ID and the name of the student. 

If we execute the code, we get the following output:

In the preceding code, the students are grouped with different classes. It shows the different students present in each class. 

With this, we have seen how operators work in LINQ. In the next section, we will look at the behind-the-scenes interfaces that make LINQ queries possible.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.176.194