From 5132f4704c505176305c2fccb7911a423be23566 Mon Sep 17 00:00:00 2001 From: Mahmoud Bakir Date: Sun, 5 Apr 2026 15:47:03 +0530 Subject: [PATCH] Add AggregateBy seedSelector examples --- xml/System.Linq/Enumerable.xml | 93 +++++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) diff --git a/xml/System.Linq/Enumerable.xml b/xml/System.Linq/Enumerable.xml index 724effb2ced..c879b848aa8 100644 --- a/xml/System.Linq/Enumerable.xml +++ b/xml/System.Linq/Enumerable.xml @@ -414,7 +414,52 @@ This method is comparable to the methods where each grouping is being aggregated into a single value as opposed to allocating a collection for each group. - + + + The following example demonstrates how to use AggregateBy with a seed selector to compute multiple values per key. + + + class Employee + { + public string Name { get; set; } + public string Department { get; set; } + public decimal Salary { get; set; } + } + + public static void AggregateBySeedSelectorExample() + { + Employee[] employees = + { + new Employee { Name = "Ali", Department = "HR", Salary = 45000 }, + new Employee { Name = "Samer", Department = "Technology", Salary = 50000 }, + new Employee { Name = "Hamed", Department = "Sales", Salary = 75000 }, + new Employee { Name = "Lina", Department = "Technology", Salary = 65000 }, + new Employee { Name = "Omar", Department = "HR", Salary = 40000 } + }; + + var result = + employees.AggregateBy( + e => e.Department, + dept => (Total: 0m, Count: 0), + (acc, e) => (acc.Total + e.Salary, acc.Count + 1) + ); + + foreach (var item in result) + { + Console.WriteLine($"{item.Key}: Total={item.Value.Total}, Count={item.Value.Count}"); + } + } + + /* + This code produces the following output: + + HR: Total=85000, Count=2 + Technology: Total=115000, Count=2 + Sales: Total=75000, Count=1 + */ + + + @@ -491,6 +536,52 @@ This method is comparable to the methods where each grouping is being aggregated into a single value as opposed to allocating a collection for each group. + + + The following example demonstrates how to use AggregateBy with a constant seed value to compute totals per key. + + + class Employee + { + public string Name { get; set; } + public string Department { get; set; } + public decimal Salary { get; set; } + } + + public static void AggregateBySeedExample() + { + Employee[] employees = + { + new Employee { Name = "Ali", Department = "HR", Salary = 45000 }, + new Employee { Name = "Samer", Department = "Technology", Salary = 50000 }, + new Employee { Name = "Hamed", Department = "Sales", Salary = 75000 }, + new Employee { Name = "Lina", Department = "Technology", Salary = 65000 }, + new Employee { Name = "Omar", Department = "HR", Salary = 40000 } + }; + + // Compute total salary per department using a constant seed + var totals = + employees.AggregateBy( + e => e.Department, + 0m, + (total, e) => total + e.Salary + ); + + foreach (var item in totals) + { + Console.WriteLine($"{item.Key}: {item.Value}"); + } + } + + /* + This code produces the following output: + + HR: 85000 + Technology: 115000 + Sales: 75000 + */ + +