Skip to content

Commit baffd43

Browse files
committed
Add AggregateBy examples to documentation per PR feedback
1 parent 5132f47 commit baffd43

File tree

2 files changed

+132
-119
lines changed

2 files changed

+132
-119
lines changed

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

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

xml/System.Linq/Enumerable.xml

Lines changed: 52 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -400,65 +400,32 @@
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-
<example>
418-
<para>
419-
The following example demonstrates how to use AggregateBy with a seed selector to compute multiple values per key.
420-
</para>
421-
<code language="csharp">
422-
class Employee
423-
{
424-
public string Name { get; set; }
425-
public string Department { get; set; }
426-
public decimal Salary { get; set; }
427-
}
428-
429-
public static void AggregateBySeedSelectorExample()
430-
{
431-
Employee[] employees =
432-
{
433-
new Employee { Name = "Ali", Department = "HR", Salary = 45000 },
434-
new Employee { Name = "Samer", Department = "Technology", Salary = 50000 },
435-
new Employee { Name = "Hamed", Department = "Sales", Salary = 75000 },
436-
new Employee { Name = "Lina", Department = "Technology", Salary = 65000 },
437-
new Employee { Name = "Omar", Department = "HR", Salary = 40000 }
438-
};
439-
440-
var result =
441-
employees.AggregateBy(
442-
e => e.Department,
443-
dept => (Total: 0m, Count: 0),
444-
(acc, e) => (acc.Total + e.Salary, acc.Count + 1)
445-
);
446-
447-
foreach (var item in result)
448-
{
449-
Console.WriteLine($"{item.Key}: Total={item.Value.Total}, Count={item.Value.Count}");
450-
}
451-
}
452-
453-
/*
454-
This code produces the following output:
455-
456-
HR: Total=85000, Count=2
457-
Technology: Total=115000, Count=2
458-
Sales: Total=75000, Count=1
459-
*/
460-
</code>
461-
</example>
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%2A> methods where each grouping is being 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>
462429
</Docs>
463430
</Member>
464431
<Member MemberName="AggregateBy&lt;TSource,TKey,TAccumulate&gt;">
@@ -523,66 +490,32 @@
523490
</Parameter>
524491
</Parameters>
525492
<Docs>
526-
<typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
527-
<typeparam name="TKey">The type of the key returned by <paramref name="keySelector" />.</typeparam>
528-
<typeparam name="TAccumulate">The type of the accumulator value.</typeparam>
529-
<param name="source">An <see cref="T:System.Collections.Generic.IEnumerable`1" /> to aggregate over.</param>
530-
<param name="keySelector">A function to extract the key for each element.</param>
531-
<param name="seed">The initial accumulator value.</param>
532-
<param name="func">An accumulator function to be invoked on each element.</param>
533-
<param name="keyComparer">An <see cref="T:System.Collections.Generic.IEqualityComparer`1" /> to compare keys with.</param>
534-
<summary>Applies an accumulator function over a sequence, grouping results by key.</summary>
535-
<returns>An enumerable containing the aggregates corresponding to each key deriving from <paramref name="source" />.</returns>
536-
<remarks>
537-
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.
538-
</remarks>
539-
<example>
540-
<para>
541-
The following example demonstrates how to use AggregateBy with a constant seed value to compute totals per key.
542-
</para>
543-
<code language="csharp">
544-
class Employee
545-
{
546-
public string Name { get; set; }
547-
public string Department { get; set; }
548-
public decimal Salary { get; set; }
549-
}
550-
551-
public static void AggregateBySeedExample()
552-
{
553-
Employee[] employees =
554-
{
555-
new Employee { Name = "Ali", Department = "HR", Salary = 45000 },
556-
new Employee { Name = "Samer", Department = "Technology", Salary = 50000 },
557-
new Employee { Name = "Hamed", Department = "Sales", Salary = 75000 },
558-
new Employee { Name = "Lina", Department = "Technology", Salary = 65000 },
559-
new Employee { Name = "Omar", Department = "HR", Salary = 40000 }
560-
};
561-
562-
// Compute total salary per department using a constant seed
563-
var totals =
564-
employees.AggregateBy(
565-
e => e.Department,
566-
0m,
567-
(total, e) => total + e.Salary
568-
);
569-
570-
foreach (var item in totals)
571-
{
572-
Console.WriteLine($"{item.Key}: {item.Value}");
573-
}
574-
}
575-
576-
/*
577-
This code produces the following output:
578-
579-
HR: 85000
580-
Technology: 115000
581-
Sales: 75000
582-
*/
583-
</code>
584-
</example>
585-
</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%2A> 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="Snippe":::
515+
516+
]]></format>
517+
</remarks>
518+
</Docs>
586519
</Member>
587520
<Member MemberName="All&lt;TSource&gt;">
588521
<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)