Introduction:
LINQ (Language-Integrated Query) is a powerful feature in C# that enables developers to query and manipulate data in a concise and expressive manner. In this blog post, we’ll explore how to leverage LINQ to group data and extract specific information from the grouped results. We’ll dive into an example scenario and provide code snippets to demonstrate the process.
Scenario:
Imagine you have a list of employees, each represented by an Employee class with properties like Name, Department, and Salary. You want to group the employees by department and then create a new list that contains the names and salaries of the employees within each department.
Code Snippet:
using System;
using System.Collections.Generic;
using System.Linq;
namespace LINQGroupByExample
{
public class Employee
{
public string Name { get; set; }
public string Department { get; set; }
public decimal Salary { get; set; }
}
public class Program
{
public static void Main()
{
// Create a list of employees
List<Employee> employees = new List<Employee>()
{
new Employee { Name = "John Doe", Department = "Engineering", Salary = 5000 },
new Employee { Name = "Jane Smith", Department = "Sales", Salary = 4000 },
new Employee { Name = "Mike Johnson", Department = "Engineering", Salary = 5500 },
new Employee { Name = "Lisa Brown", Department = "HR", Salary = 4500 },
new Employee { Name = "David Wilson", Department = "Sales", Salary = 4200 }
};
// Group employees by department and create a new list
List<(string Department, List<(string Name, decimal Salary)> Employees)> departmentList = employees
.GroupBy(emp => emp.Department)
.Select(group => (
Department: group.Key,
Employees: group.Select(emp => (Name: emp.Name, Salary: emp.Salary)).ToList()
))
.ToList();
// Print the department and its employees' names and salaries
foreach (var department in departmentList)
{
Console.WriteLine($"Department: {department.Department}");
foreach (var employee in department.Employees)
{
Console.WriteLine($"Name: {employee.Name}, Salary: {employee.Salary}");
}
Console.WriteLine();
}
}
}
}
Output:
Department: Engineering
Name: John Doe, Salary: 5000
Name: Mike Johnson, Salary: 5500
Department: Sales
Name: Jane Smith, Salary: 4000
Name: David Wilson, Salary: 4200
Department: HR
Name: Lisa Brown, Salary: 4500
Explanation: In the code snippet, we start by creating a list of employees. We then use the LINQ GroupBy
method to group the employees by their department. Next, we use the Select
method to create a new list where each item represents a department along with a list of employees within that department. Finally, we iterate over the departmentList
and print the department name, along with the names and salaries of the employees within each department.
Conclusion:
LINQ is a powerful tool in C# that allows developers to perform complex queries and manipulations on data with ease. By utilizing LINQ’s GroupBy
and Select
methods, we can efficiently group data and extract specific information from the grouped results. This enables us to create customized lists and gain valuable insights from our data.