• Javascript
  • Python
  • Go

GroupBy Two Values in C# List<>

GroupBy Two Values in C# List&lt;&gt; When working with large datasets, it is often necessary to group the data based on certain criteria. I...

GroupBy Two Values in C# List<>

When working with large datasets, it is often necessary to group the data based on certain criteria. In C#, the List<> class provides a convenient way to store and manipulate collections of data. However, what if you need to group the data by two different values? This is where the GroupBy method comes in handy.

The GroupBy method allows you to group the elements in a List<> based on a key. This key can be a single value or a combination of multiple values. In this article, we will explore how to use the GroupBy method to group data in a List<> by two different values.

Let's start with a simple example. Suppose we have a List<> of Employee objects, each with properties for name, department, and salary. Our goal is to group the employees by department and then by salary range. To achieve this, we will first create a class to represent an employee.

```C#

public class Employee

{

public string Name { get; set; }

public string Department { get; set; }

public int Salary { get; set; }

}

```

Next, we will create a List<> of Employee objects and populate it with some sample data.

```C#

List<Employee> employees = new List<Employee>()

{

new Employee { Name = "John", Department = "Marketing", Salary = 50000 },

new Employee { Name = "Emily", Department = "HR", Salary = 60000 },

new Employee { Name = "Mark", Department = "Marketing", Salary = 55000 },

new Employee { Name = "Sara", Department = "IT", Salary = 70000 },

new Employee { Name = "David", Department = "HR", Salary = 65000 },

new Employee { Name = "Rachel", Department = "IT", Salary = 75000 }

};

```

Now, let's use the GroupBy method to group the employees by department and then by salary range. We will first group the employees by department and then use another GroupBy method to further group them by salary range.

```C#

var groupedEmployees = employees.GroupBy(e => e.Department)

.SelectMany(g => g.GroupBy(e => e.Salary / 10000)

.SelectMany(g => g));

foreach (var group in groupedEmployees)

{

Console.WriteLine($"{group.Name} - {group.Department} - {group.Salary}");

}

```

The output of the above code will be:

```C#

John - Marketing - 50000

Mark - Marketing - 55000

Emily - HR - 60000

David - HR - 65000

Sara - IT - 70000

Rachel - IT - 75000

```

As you can see, the employees have been grouped first by department and then by salary range (in increments of 10000). This is achieved by using the SelectMany method to flatten the nested groupings.

You can also use anonymous types to group the data by multiple values. For example, if we want to group the employees by department and salary range, but only display the total number of employees in each group, we can modify our code as follows:

```C#

var groupedEmployees = employees.GroupBy(e => new { e.Department, SalaryRange = e.Salary / 10000 })

.Select(g => new

{

Related Articles

Array Subset Check

Arrays are an essential part of programming and are used to store a collection of values. They allow us to efficiently organize and manipula...

Efficient LINQ Query on a DataTable

In the world of data processing, efficiency is key. As more and more data is being generated and collected, the need for efficient methods o...