Skip to content

Commit a91dcc1

Browse files
Merge pull request #12516 from dotnet/main
Merge main into live
2 parents b3cf340 + 618326e commit a91dcc1

3 files changed

Lines changed: 127 additions & 30 deletions

File tree

snippets/csharp/System.Linq/Enumerable/AggregateTSource/Enumerable.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net8.0</TargetFramework>
5+
<TargetFramework>net9.0</TargetFramework>
66
</PropertyGroup>
77

88
</Project>

snippets/csharp/System.Linq/Enumerable/AggregateTSource/enumerable.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3204,5 +3204,78 @@ static void TakeLast()
32043204
#endif
32053205
}
32063206
#endregion
3207+
3208+
#region AggregateBy
3209+
static class AggregateBy
3210+
{
3211+
// <Snippet205>
3212+
public static void AggregateBySeedSelectorExample()
3213+
{
3214+
(string Name, string Department, decimal Salary)[] employees =
3215+
{
3216+
("Ali", "HR", 45000),
3217+
("Samer", "Technology", 50000),
3218+
("Hamed", "Sales", 75000),
3219+
("Lina", "Technology", 65000),
3220+
("Omar", "HR", 40000)
3221+
};
3222+
3223+
var result =
3224+
employees.AggregateBy(
3225+
e => e.Department,
3226+
dept => (Total: 0m, Count: 0),
3227+
(acc, e) => (acc.Total + e.Salary, acc.Count + 1)
3228+
);
3229+
3230+
foreach (var item in result)
3231+
{
3232+
Console.WriteLine($"{item.Key}: Total={item.Value.Total}, Count={item.Value.Count}");
3233+
}
3234+
3235+
/*
3236+
This code produces the following output:
3237+
3238+
HR: Total=85000, Count=2
3239+
Technology: Total=115000, Count=2
3240+
Sales: Total=75000, Count=1
3241+
*/
3242+
}
3243+
// </Snippet205>
3244+
3245+
// <Snippet206>
3246+
public static void AggregateBySeedExample()
3247+
{
3248+
(string Name, string Department, decimal Salary)[] employees =
3249+
{
3250+
("Ali", "HR", 45000),
3251+
("Samer", "Technology", 50000),
3252+
("Hamed", "Sales", 75000),
3253+
("Lina", "Technology", 65000),
3254+
("Omar", "HR", 40000)
3255+
};
3256+
3257+
var totals =
3258+
employees.AggregateBy(
3259+
e => e.Department,
3260+
0m,
3261+
(total, e) => total + e.Salary
3262+
);
3263+
3264+
foreach (var item in totals)
3265+
{
3266+
Console.WriteLine($"{item.Key}: {item.Value}");
3267+
}
3268+
3269+
/*
3270+
This code produces the following output:
3271+
3272+
HR: 85000
3273+
Technology: 115000
3274+
Sales: 75000
3275+
*/
3276+
}
3277+
// </Snippet206>
3278+
}
3279+
#endregion
32073280
}
32083281
}

xml/System.Linq/Enumerable.xml

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -400,21 +400,33 @@
400400
</Attributes>
401401
</Parameter>
402402
</Parameters>
403-
<Docs>
404-
<typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
405-
<typeparam name="TKey">The type of the key returned by <paramref name="keySelector" />.</typeparam>
406-
<typeparam name="TAccumulate">The type of the accumulator value.</typeparam>
407-
<param name="source">An <see cref="T:System.Collections.Generic.IEnumerable`1" /> to aggregate over.</param>
408-
<param name="keySelector">A function to extract the key for each element.</param>
409-
<param name="seedSelector">A factory for the initial accumulator value.</param>
410-
<param name="func">An accumulator function to be invoked on each element.</param>
411-
<param name="keyComparer">An <see cref="T:System.Collections.Generic.IEqualityComparer`1" /> to compare keys with.</param>
412-
<summary>Applies an accumulator function over a sequence, grouping results by key.</summary>
413-
<returns>An enumerable containing the aggregates corresponding to each key deriving from <paramref name="source" />.</returns>
414-
<remarks>
415-
This method is comparable to the <see cref="M:System.Linq.Enumerable.GroupBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1})" /> methods where each grouping is being aggregated into a single value as opposed to allocating a collection for each group.
416-
</remarks>
417-
</Docs>
403+
<Docs>
404+
<typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
405+
<typeparam name="TKey">The type of the key returned by <paramref name="keySelector" />.</typeparam>
406+
<typeparam name="TAccumulate">The type of the accumulator value.</typeparam>
407+
<param name="source">An <see cref="T:System.Collections.Generic.IEnumerable`1" /> to aggregate over.</param>
408+
<param name="keySelector">A function to extract the key for each element.</param>
409+
<param name="seedSelector">A factory for the initial accumulator value.</param>
410+
<param name="func">An accumulator function to be invoked on each element.</param>
411+
<param name="keyComparer">An <see cref="T:System.Collections.Generic.IEqualityComparer`1" /> to compare keys with.</param>
412+
<summary>Applies an accumulator function over a sequence, grouping results by key.</summary>
413+
<returns>An enumerable containing the aggregates corresponding to each key deriving from <paramref name="source" />.</returns>
414+
<remarks>
415+
<format type="text/markdown"><![CDATA[
416+
417+
## Remarks
418+
419+
This method is comparable to the <xref:System.Linq.Enumerable.GroupBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1})> methods where each grouping is aggregated into a single value as opposed to allocating a collection for each group.
420+
421+
## Examples
422+
423+
The following example demonstrates how to use `AggregateBy` with a seed selector to compute multiple values per key.
424+
425+
:::code language="csharp" source="~/snippets/csharp/System.Linq/Enumerable/AggregateTSource/enumerable.cs" id="Snippet205":::
426+
427+
]]></format>
428+
</remarks>
429+
</Docs>
418430
</Member>
419431
<Member MemberName="AggregateBy&lt;TSource,TKey,TAccumulate&gt;">
420432
<MemberSignature Language="C#" Value="public static System.Collections.Generic.IEnumerable&lt;System.Collections.Generic.KeyValuePair&lt;TKey,TAccumulate&gt;&gt; AggregateBy&lt;TSource,TKey,TAccumulate&gt; (this System.Collections.Generic.IEnumerable&lt;TSource&gt; source, Func&lt;TSource,TKey&gt; keySelector, TAccumulate seed, Func&lt;TAccumulate,TSource,TAccumulate&gt; func, System.Collections.Generic.IEqualityComparer&lt;TKey&gt;? keyComparer = default);" />
@@ -478,20 +490,32 @@
478490
</Parameter>
479491
</Parameters>
480492
<Docs>
481-
<typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
482-
<typeparam name="TKey">The type of the key returned by <paramref name="keySelector" />.</typeparam>
483-
<typeparam name="TAccumulate">The type of the accumulator value.</typeparam>
484-
<param name="source">An <see cref="T:System.Collections.Generic.IEnumerable`1" /> to aggregate over.</param>
485-
<param name="keySelector">A function to extract the key for each element.</param>
486-
<param name="seed">The initial accumulator value.</param>
487-
<param name="func">An accumulator function to be invoked on each element.</param>
488-
<param name="keyComparer">An <see cref="T:System.Collections.Generic.IEqualityComparer`1" /> to compare keys with.</param>
489-
<summary>Applies an accumulator function over a sequence, grouping results by key.</summary>
490-
<returns>An enumerable containing the aggregates corresponding to each key deriving from <paramref name="source" />.</returns>
491-
<remarks>
492-
This method is comparable to the <see cref="M:System.Linq.Enumerable.GroupBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1})" /> methods where each grouping is being aggregated into a single value as opposed to allocating a collection for each group.
493-
</remarks>
494-
</Docs>
493+
<typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
494+
<typeparam name="TKey">The type of the key returned by <paramref name="keySelector" />.</typeparam>
495+
<typeparam name="TAccumulate">The type of the accumulator value.</typeparam>
496+
<param name="source">An <see cref="T:System.Collections.Generic.IEnumerable`1" /> to aggregate over.</param>
497+
<param name="keySelector">A function to extract the key for each element.</param>
498+
<param name="seed">The initial accumulator value.</param>
499+
<param name="func">An accumulator function to be invoked on each element.</param>
500+
<param name="keyComparer">An <see cref="T:System.Collections.Generic.IEqualityComparer`1" /> to compare keys with.</param>
501+
<summary>Applies an accumulator function over a sequence, grouping results by key.</summary>
502+
<returns>An enumerable containing the aggregates corresponding to each key deriving from <paramref name="source" />.</returns>
503+
<remarks>
504+
<format type="text/markdown"><![CDATA[
505+
506+
## Remarks
507+
508+
This method is comparable to the <xref:System.Linq.Enumerable.GroupBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1})> methods where each grouping is being aggregated into a single value as opposed to allocating a collection for each group.
509+
510+
## Examples
511+
512+
The following example demonstrates how to use `AggregateBy` with a constant seed value to compute totals per key.
513+
514+
:::code language="csharp" source="~/snippets/csharp/System.Linq/Enumerable/AggregateTSource/enumerable.cs" id="Snippet206":::
515+
516+
]]></format>
517+
</remarks>
518+
</Docs>
495519
</Member>
496520
<Member MemberName="All&lt;TSource&gt;">
497521
<MemberSignature Language="C#" Value="public static bool All&lt;TSource&gt; (this System.Collections.Generic.IEnumerable&lt;TSource&gt; source, Func&lt;TSource,bool&gt; predicate);" />

0 commit comments

Comments
 (0)