C# Keywords Tutorial Part 41: group

C# Keywords Tutorial Part 41: group

C# is a powerful programming language that is widely used by developers across the globe. One of its unique features is the “group” keyword, which allows developers to group objects based on a common property. In this blog post, we will explore the “group” keyword in C# with code examples.

The “group” keyword in C# is used to group a collection of objects based on a common property. It is often used with the “by” keyword, which specifies the property to group by. Let’s take a look at a simple example to understand this better.

Suppose we have a list of employees with their name, department, and salary. We can use the “group” keyword to group the employees by their department, as shown below:

List<Employee> employees = new List<Employee>
{
    new Employee { Name = "John Doe", Department = "IT", Salary = 5000 },
    new Employee { Name = "Jane Smith", Department = "HR", Salary = 6000 },
    new Employee { Name = "Bob Johnson", Department = "IT", Salary = 5500 },
    new Employee { Name = "Mary Jones", Department = "Sales", Salary = 7000 },
    new Employee { Name = "Tom Brown", Department = "IT", Salary = 4800 }
};

var groupedEmployees = from e in employees
                       group e by e.Department;

foreach (var group in groupedEmployees)
{
    Console.WriteLine("Department: " + group.Key);
    foreach (var employee in group)
    {
        Console.WriteLine("\t" + employee.Name + "\t" + employee.Salary);
    }
}

In this example, we create a list of employees with their name, department, and salary. We then use the “group” keyword to group the employees by their department. The resulting collection is then iterated over and each group is printed along with its employees’ names and salaries.

The output of this program would be:

Department: IT
    John Doe     5000
    Bob Johnson  5500
    Tom Brown    4800
Department: HR
    Jane Smith   6000
Department: Sales
    Mary Jones   7000

As we can see, the employees are grouped by their department, and each department is printed along with its employees.

The “group” keyword can also be used with LINQ to perform complex queries. Let’s take a look at another example:

var highSalaryDepartments = from e in employees
                            group e by e.Department into g
                            where g.Max(x => x.Salary) > 6000
                            select new { Department = g.Key, MaxSalary = g.Max(x => x.Salary) };

foreach (var department in highSalaryDepartments)
{
    Console.WriteLine("Department: " + department.Department + " Max Salary: " + department.MaxSalary);
}

In this example, we group the employees by their department and then filter out the departments where the maximum salary is less than or equal to 6000. We then create a new anonymous object with the department name and the maximum salary for that department. The resulting collection is then iterated over and each department is printed along with its maximum salary.

The output of this program would be:

Department: HR Max Salary: 6000
Department: Sales Max Salary: 7000

As we can see, the “group” keyword can be used with LINQ to perform complex queries and retrieve meaningful data.

In conclusion, the “group” keyword in C# is a powerful tool that allows developers to group objects based on a common property. It can be used to perform simple

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *