📅  最后修改于: 2023-12-03 15:17:19.748000             🧑  作者: Mango
LINQ (Language-Integrated Query) is a powerful tool in .NET programming that allows for easy querying of data from various data sources, including arrays, collections, and databases. One common task in LINQ is grouping data by a certain property and then performing an aggregate function, such as finding the sum of all values in each group. In this tutorial, we will explore how to use LINQ to group data by a property and then find the sum of that property for each group.
For this tutorial, we will be using the following sample data:
List<Employee> employees = new List<Employee>
{
new Employee { Name = "John", Department = "Sales", Salary = 50000 },
new Employee { Name = "Jane", Department = "Sales", Salary = 60000 },
new Employee { Name = "Jim", Department = "Marketing", Salary = 45000 },
new Employee { Name = "Jack", Department = "Marketing", Salary = 55000 },
new Employee { Name = "Jill", Department = "Marketing", Salary = 65000 },
new Employee { Name = "Joe", Department = "HR", Salary = 40000 },
new Employee { Name = "Jerry", Department = "HR", Salary = 45000 },
new Employee { Name = "Jenny", Department = "IT", Salary = 55000 },
new Employee { Name = "Justin", Department = "IT", Salary = 65000 }
};
This is a simple list of Employee objects, each with a Name, Department, and Salary property.
Our first step is to group the employees by department. We can use the LINQ GroupBy function to achieve this:
var grouped = employees.GroupBy(e => e.Department);
This will give us an IEnumerable of IGrouping objects, each representing a group of employees with the same department.
Now that we have our groups, we want to find the sum of salaries for each group. We can use the LINQ Sum function to achieve this:
var sums = grouped.Select(g => new { Department = g.Key, SalarySum = g.Sum(e => e.Salary) });
This will give us an IEnumerable of anonymous objects, each with a Department and SalarySum property. The Department property is the key of the group, and the SalarySum property is the sum of all salaries in that group.
Here is the complete code for grouping the employees by department and finding the sum of salaries for each group:
List<Employee> employees = new List<Employee>
{
new Employee { Name = "John", Department = "Sales", Salary = 50000 },
new Employee { Name = "Jane", Department = "Sales", Salary = 60000 },
new Employee { Name = "Jim", Department = "Marketing", Salary = 45000 },
new Employee { Name = "Jack", Department = "Marketing", Salary = 55000 },
new Employee { Name = "Jill", Department = "Marketing", Salary = 65000 },
new Employee { Name = "Joe", Department = "HR", Salary = 40000 },
new Employee { Name = "Jerry", Department = "HR", Salary = 45000 },
new Employee { Name = "Jenny", Department = "IT", Salary = 55000 },
new Employee { Name = "Justin", Department = "IT", Salary = 65000 }
};
var grouped = employees.GroupBy(e => e.Department);
var sums = grouped.Select(g => new { Department = g.Key, SalarySum = g.Sum(e => e.Salary) });
foreach (var sum in sums)
{
Console.WriteLine($"Department: {sum.Department}, Salary Sum: {sum.SalarySum}");
}
This will output the following:
Department: Sales, Salary Sum: 110000
Department: Marketing, Salary Sum: 165000
Department: HR, Salary Sum: 85000
Department: IT, Salary Sum: 120000
Using LINQ to group data by a property and then find the sum of that property for each group is a powerful tool for analyzing data. With just a few lines of code, we can easily perform complex operations on large datasets.