Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3204,5 +3204,85 @@ static void TakeLast()
#endif
}
#endregion

#region AggregateBy
static class AggregateBy
{
// <Snippet205>
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
*/
}
// </Snippet205>

// <Snippet206>
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 }
};

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
*/
}
// </Snippet206>
}
#endregion
}
}
82 changes: 53 additions & 29 deletions xml/System.Linq/Enumerable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -400,21 +400,33 @@
</Attributes>
</Parameter>
</Parameters>
<Docs>
<typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
<typeparam name="TKey">The type of the key returned by <paramref name="keySelector" />.</typeparam>
<typeparam name="TAccumulate">The type of the accumulator value.</typeparam>
<param name="source">An <see cref="T:System.Collections.Generic.IEnumerable`1" /> to aggregate over.</param>
<param name="keySelector">A function to extract the key for each element.</param>
<param name="seedSelector">A factory for the initial accumulator value.</param>
<param name="func">An accumulator function to be invoked on each element.</param>
<param name="keyComparer">An <see cref="T:System.Collections.Generic.IEqualityComparer`1" /> to compare keys with.</param>
<summary>Applies an accumulator function over a sequence, grouping results by key.</summary>
<returns>An enumerable containing the aggregates corresponding to each key deriving from <paramref name="source" />.</returns>
<remarks>
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.
</remarks>
</Docs>
<Docs>
<typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
<typeparam name="TKey">The type of the key returned by <paramref name="keySelector" />.</typeparam>
<typeparam name="TAccumulate">The type of the accumulator value.</typeparam>
<param name="source">An <see cref="T:System.Collections.Generic.IEnumerable`1" /> to aggregate over.</param>
<param name="keySelector">A function to extract the key for each element.</param>
<param name="seedSelector">A factory for the initial accumulator value.</param>
<param name="func">An accumulator function to be invoked on each element.</param>
<param name="keyComparer">An <see cref="T:System.Collections.Generic.IEqualityComparer`1" /> to compare keys with.</param>
<summary>Applies an accumulator function over a sequence, grouping results by key.</summary>
<returns>An enumerable containing the aggregates corresponding to each key deriving from <paramref name="source" />.</returns>
<remarks>
<format type="text/markdown"><![CDATA[

## Remarks

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.

## 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":::

]]></format>
</remarks>
</Docs>
</Member>
<Member MemberName="AggregateBy&lt;TSource,TKey,TAccumulate&gt;">
<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);" />
Expand Down Expand Up @@ -478,20 +490,32 @@
</Parameter>
</Parameters>
<Docs>
<typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
<typeparam name="TKey">The type of the key returned by <paramref name="keySelector" />.</typeparam>
<typeparam name="TAccumulate">The type of the accumulator value.</typeparam>
<param name="source">An <see cref="T:System.Collections.Generic.IEnumerable`1" /> to aggregate over.</param>
<param name="keySelector">A function to extract the key for each element.</param>
<param name="seed">The initial accumulator value.</param>
<param name="func">An accumulator function to be invoked on each element.</param>
<param name="keyComparer">An <see cref="T:System.Collections.Generic.IEqualityComparer`1" /> to compare keys with.</param>
<summary>Applies an accumulator function over a sequence, grouping results by key.</summary>
<returns>An enumerable containing the aggregates corresponding to each key deriving from <paramref name="source" />.</returns>
<remarks>
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.
</remarks>
</Docs>
<typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
<typeparam name="TKey">The type of the key returned by <paramref name="keySelector" />.</typeparam>
<typeparam name="TAccumulate">The type of the accumulator value.</typeparam>
<param name="source">An <see cref="T:System.Collections.Generic.IEnumerable`1" /> to aggregate over.</param>
<param name="keySelector">A function to extract the key for each element.</param>
<param name="seed">The initial accumulator value.</param>
<param name="func">An accumulator function to be invoked on each element.</param>
<param name="keyComparer">An <see cref="T:System.Collections.Generic.IEqualityComparer`1" /> to compare keys with.</param>
<summary>Applies an accumulator function over a sequence, grouping results by key.</summary>
<returns>An enumerable containing the aggregates corresponding to each key deriving from <paramref name="source" />.</returns>
<remarks>
<format type="text/markdown"><![CDATA[

## Remarks

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.

## 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":::

]]></format>
</remarks>
</Docs>
</Member>
<Member MemberName="All&lt;TSource&gt;">
<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);" />
Expand Down
Loading