Home

LINQ Keywords: group

       

Description

The group...by operation is used to create a list where the values are grouped by categories. For example, if you have a list of students, you may want the list to be organized by gender. The formula to follow is:

var SubListName = from ValueHolder in List group ValueHolder by Category;

 Here is an example:

using System;
using System.Linq;
using System.Collections.Generic;

public class Exercise
{
    public static int Main()
    {
        var students = new Student[]
    	{
            new Student(82495, "Carlton", "Blanchard", 'M'),
            new Student(20857, "Jerrie", "Sachs", 'U'),
            new Student(20935, "Charlotte", "O'Keefe", 'F'),
            new Student(79274, "Christine", "Burns", 'F'),
            new Student(79204, "Bobbie", "Swanson"),
            new Student(14815, "Marianne", "Swanson", 'F'),
            new Student(24958, "Jeannette", "Perkins", 'F'),
            new Student(24759, "Pierrette", "Perkins", 'F'),
            new Student(92804, "Charles", "Pressmann", 'M'),
            new Student(80074, "Alain", "Goodson", 'M')
    	};

        var classroom = from pupils
                        in students
                        group pupils by pupils.Gender;
        
        Console.WriteLine("+========+============+===========+=======+");
        Console.WriteLine("| Std # | First Name | Last Name | Gender |");
        foreach (var stds in classroom)
        {
            foreach (var pupil in stds)
            {
                Console.WriteLine("+-------+------------+-----------+--------+");
                Console.WriteLine("| {0,5} | {1,-10} | {2,-9} | {3,4}   |",
                                  pupil.StudentNumber, pupil.FirstName,
                                  pupil.LastName, pupil.Gender);
            }
        }

        Console.WriteLine("+=======+============+===========+========+");

        Console.WriteLine();
        return 0;
    }
}

public class Student
{
    public int    StudentNumber;
    public string FirstName;
    public string LastName;
    public char Gender;

    public Student(int number = 0,
                   string firstName = "Leslie",
                   string lastName = "Doe",
                   char   gdr = 'U')
    {
        StudentNumber = number;
        FirstName = firstName;
        LastName = lastName;
        Gender = gdr;
    }
}

This would produce:

Group By

To restrict the list of records in the result, you can add a where condition. Here is an example:

public class Exercise
{
    public static int Main()
    {
        var students = new Student[]
    	{
            new Student(82495, "Carlton", "Blanchard", 'M'),
            new Student(20857, "Jerrie", "Sachs", 'U'),
            new Student(20935, "Charlotte", "O'Keefe", 'F'),
            new Student(79274, "Christine", "Burns", 'F'),
            new Student(79204, "Bobbie", "Swanson"),
            new Student(14815, "Marianne", "Swanson", 'F'),
            new Student(24958, "Jeannette", "Perkins", 'F'),
            new Student(24759, "Pierrette", "Perkins", 'F'),
            new Student(92804, "Charles", "Pressmann", 'M'),
            new Student(80074, "Alain", "Goodson", 'M')
    	};

        var classroom = from pupils
                        in students
                        where pupils.FirstName.StartsWith("C")
                        group pupils by pupils.Gender;
        
        Console.WriteLine("+========+============+===========+=======+");
        Console.WriteLine("| Std # | First Name | Last Name | Gender |");
        foreach (var stds in classroom)
        {
            foreach (var pupil in stds)
            {
                Console.WriteLine("+-------+------------+-----------+--------+");
                Console.WriteLine("| {0,5} | {1,-10} | {2,-9} | {3,4}   |",
                                  pupil.StudentNumber, pupil.FirstName,
                                  pupil.LastName, pupil.Gender);
            }
        }
        Console.WriteLine("+=======+============+===========+========+");

        Console.WriteLine();
        return 0;
    }
}

This would produce:

Group By

 


Home Copyright © 2010 FunctionX, Inc.