diff --git a/snippets/csharp/System.Linq/Enumerable/AggregateTSource/Enumerable.csproj b/snippets/csharp/System.Linq/Enumerable/AggregateTSource/Enumerable.csproj index a269962b552..92e46ddaccf 100644 --- a/snippets/csharp/System.Linq/Enumerable/AggregateTSource/Enumerable.csproj +++ b/snippets/csharp/System.Linq/Enumerable/AggregateTSource/Enumerable.csproj @@ -2,7 +2,7 @@ Exe - net8.0 + net9.0 diff --git a/snippets/csharp/System.Linq/Enumerable/AggregateTSource/enumerable.cs b/snippets/csharp/System.Linq/Enumerable/AggregateTSource/enumerable.cs index c55f5ec6902..16625939d54 100644 --- a/snippets/csharp/System.Linq/Enumerable/AggregateTSource/enumerable.cs +++ b/snippets/csharp/System.Linq/Enumerable/AggregateTSource/enumerable.cs @@ -3204,5 +3204,78 @@ static void TakeLast() #endif } #endregion + + #region AggregateBy + static class AggregateBy + { + // + public static void AggregateBySeedSelectorExample() + { + (string Name, string Department, decimal Salary)[] employees = + { + ("Ali", "HR", 45000), + ("Samer", "Technology", 50000), + ("Hamed", "Sales", 75000), + ("Lina", "Technology", 65000), + ("Omar", "HR", 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 + */ + } + // + + // + public static void AggregateBySeedExample() + { + (string Name, string Department, decimal Salary)[] employees = + { + ("Ali", "HR", 45000), + ("Samer", "Technology", 50000), + ("Hamed", "Sales", 75000), + ("Lina", "Technology", 65000), + ("Omar", "HR", 40000) + }; + + 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 + */ + } + // + } + #endregion } } diff --git a/xml/System.Linq/Enumerable.xml b/xml/System.Linq/Enumerable.xml index 724effb2ced..a5920f035ae 100644 --- a/xml/System.Linq/Enumerable.xml +++ b/xml/System.Linq/Enumerable.xml @@ -400,21 +400,33 @@ - - The type of the elements of . - The type of the key returned by . - The type of the accumulator value. - An to aggregate over. - A function to extract the key for each element. - A factory for the initial accumulator value. - An accumulator function to be invoked on each element. - An to compare keys with. - Applies an accumulator function over a sequence, grouping results by key. - An enumerable containing the aggregates corresponding to each key deriving from . - - 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 type of the elements of . + The type of the key returned by . + The type of the accumulator value. + An to aggregate over. + A function to extract the key for each element. + A factory for the initial accumulator value. + An accumulator function to be invoked on each element. + An to compare keys with. + Applies an accumulator function over a sequence, grouping results by key. + An enumerable containing the aggregates corresponding to each key deriving from . + + methods where each grouping is aggregated into a single value as opposed to allocating a collection for each group. + +## Examples + +The following example demonstrates how to use `AggregateBy` with a seed selector to compute multiple values per key. + +:::code language="csharp" source="~/snippets/csharp/System.Linq/Enumerable/AggregateTSource/enumerable.cs" id="Snippet205"::: + + ]]> + + @@ -478,20 +490,32 @@ - The type of the elements of . - The type of the key returned by . - The type of the accumulator value. - An to aggregate over. - A function to extract the key for each element. - The initial accumulator value. - An accumulator function to be invoked on each element. - An to compare keys with. - Applies an accumulator function over a sequence, grouping results by key. - An enumerable containing the aggregates corresponding to each key deriving from . - - 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 type of the elements of . + The type of the key returned by . + The type of the accumulator value. + An to aggregate over. + A function to extract the key for each element. + The initial accumulator value. + An accumulator function to be invoked on each element. + An to compare keys with. + Applies an accumulator function over a sequence, grouping results by key. + An enumerable containing the aggregates corresponding to each key deriving from . + + methods where each grouping is being aggregated into a single value as opposed to allocating a collection for each group. + +## Examples + +The following example demonstrates how to use `AggregateBy` with a constant seed value to compute totals per key. + +:::code language="csharp" source="~/snippets/csharp/System.Linq/Enumerable/AggregateTSource/enumerable.cs" id="Snippet206"::: + + ]]> + +