About
In this post, we are exploring about AggregateBy LINQ method introduced in .NET 9. This is very helpful and being used in scenarios of aggregate operations such as sum, average and grouping of data based on custom conditions.
In Earlier post, Please look into about other feature discussed about CountBy in LINQ 9.
So, Lets go ahead to discuss/understand about AggregateBy in LINQ.
Scenario
Lets consider following scenario and understand implementation of this before and after .NET 9 approach.
"Get sum of employee merit score points based on job level"
Employee class
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public int YearJoin { get; set; }
public int JobLevel { get; set; }
public int MeritPoints { get; set; }
}
Setup Employee List with Sample Data
var employees = new List {
new Employee(){ Id = 11, Name = "Ram", YearJoin = 2015, JobLevel = 6, MeritPoints = 10 },
new Employee(){ Id = 12, Name = "Bheem", YearJoin = 2012, JobLevel = 5, MeritPoints = 20 },
new Employee(){Id = 13, Name = "Lakshman", YearJoin = 2015, JobLevel = 6, MeritPoints = 30},
new Employee(){Id = 14, Name = "Hanu", YearJoin = 2016, JobLevel = 6, MeritPoints = 40},
new Employee(){Id = 15, Name = "Dev", YearJoin = 2024, JobLevel = 6, MeritPoints = 50},
new Employee(){Id = 16, Name = "Nandan", YearJoin = 2012, JobLevel = 5, MeritPoints = 60},
new Employee(){Id = 17, Name = "Krish", YearJoin = 2012, JobLevel = 5, MeritPoints = 70},
new Employee(){Id = 18, Name = "Hash", YearJoin = 2015, JobLevel = 6, MeritPoints = 80},
};
Before .NET 9
//BEFORE .NET 9 Example
// Aggregate MeritPoints by Job Level using GroupBy and Aggregate
var meritPointsByEmpJobLevel = employees
.GroupBy(user => user.JobLevel) // Group users by their Job Level
.Select(group => new
{
JobLevel = group.Key,
TotalMeritPoints = group.Sum(user => user.MeritPoints)
}
); // Aggregate Merit Points for each Job Level
// Print the results
foreach (var jobLevelAggregate in meritPointsByEmpJobLevel)
{
Console.WriteLine($"Total merit points per each employee level " +
$"{jobLevelAggregate.JobLevel} is {jobLevelAggregate.TotalMeritPoints}");
}
Aforesaid code example, we did the following steps.
1. Group the employees based on Job level.
2. Select the Job level as Key and Sum of Meri points on respective job level.
3. Display the results in fore ach loop based on select results.
.NET 9
var totalEmpMeritPointsByLevel = employees.AggregateBy(e => e.JobLevel,
seed: 0, (acc, meritPoints) => acc + meritPoints.MeritPoints);
foreach (var meritPointByLevel in totalEmpMeritPointsByLevel)
{
Console.WriteLine($"Total merit points per each employee level " +
$"{meritPointByLevel.Key} is {meritPointByLevel.Value}");
}
The aforesaid .NET 9 code example, we achieved all in one go with AggregateBy example. The code also very clean and crsip.
Syntax:
IEnumerable<(TKey Key, TAccumulate Aggregate)> AggregateBy(
this IEnumerable source,
Func keySelector,
Func seedFactory,
Func aggregator
);
Screenshots
Code
Output
Summary
In this post, we explored about AggregateBy LINQ feature with ease of use and code examples. Those are really helpful to improve developer productivity and ease of understanding and performance of applications.
Thanks for reading and Stay tuned for more articles!!
No comments:
Post a Comment