LINQ Aggregate Operator in C# with Examples – Programming, Pseudocode Example, C# Programming Example
C# Linq

LINQ Aggregate Operator in C# with Examples

The Aggregate operator is a LINQ query operator that applies a function to each element of a sequence and accumulates the result. The function takes two arguments: the accumulator and the current element of the sequence.

Here are some examples of how you might use the Aggregate operator in C#:

Example 1: Summing a list of integers:

Example 2: Multiplying a list of integers:

Example 3: Concatenating a list of strings:

Example 4: Finding the maximum value in a list of integers:

Example 5: Finding the minimum value in a list of integers:

In these examples, the Aggregate operator is used to apply a function to each element of a sequence and accumulate the result. The function takes two arguments: the accumulator and the current element of the sequence. In the first example, the Aggregate operator is used to sum a list of integers. In the second example, the Aggregate operator is used to multiply a list of integers. In the third example, the Aggregate operator is used to concatenate a list of strings. In the fourth and fifth examples, the Aggregate operator is used to find the maximum and minimum value in a list of integers respectively.

It’s worth noting that the Aggregate operator returns a single value, and it can take an initial seed value as an optional parameter which is used as the initial accumulator value. This can be useful for more complex aggregation scenarios.

1 Comment

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.