Understanding the Aggregate function performs an operation on each list element while considering its primary operations. This involves executing the action on the first and second elements and carrying the resultant value forward. Subsequently, it operates on the previous result, the third element, etc.
For instance, let’s consider the following examples to illustrate the application of the Aggregate function.
Adding numbers
Suppose we have an array of numbers: [1, 2, 3, 4]. To calculate their sum, we can utilize the Aggregate function as follows:
int[] nums = {1, 2, 3, 4};
int sum = nums.Aggregate((a, b) => a + b);
Console.WriteLine("The sum of the numbers is: " + sum); // Output: The sum of the numbers is: 10 (1 + 2 + 3 + 4)
In this scenario, the function adds 1 and 2 to yield 3. It then adds the previous result (3) to the next element in the sequence (3), resulting in 6. This process continues until all elements have been considered, ultimately yielding the sum of 10.
Creating a CSV from an array of strings
Suppose we have an array of strings: [“a”, “b”, “c”, “d”]. We can employ the Aggregate function to generate a comma-separated value (CSV) representation of the array:
string[] chars = {"a", "b", "c", "d"};
string csv = chars.Aggregate((a, b) => $"{a},{b}");
Console.WriteLine("The CSV representation is: " + csv); // Output: The CSV representation is: a,b,c,d
In this example, the function concatenates each element with a comma, sequentially building the CSV representation. For instance, it combines “a” with “,” resulting in “a,”. Subsequently, it appends “b” to yield “a,b,”. This process continues until all elements have been processed, resulting in the final CSV string “a,b,c,d”.
Multiplying numbers
Additionally, the Aggregate function provides an overload that accepts a seed value. Consider an array of numbers: [10, 20, 30, 40]. Aggregate functions can also be utilized to perform multiplication:
int[] multipliers = {10, 20, 30, 40};
int seed = 5;
int multiplied = multipliers.Aggregate(seed, (a, b) => a * b);
Console.WriteLine("The result of multiplying the numbers is: " + multiplied); // Output: The result of multiplying the numbers is: 1200000 ((((5 * 10) * 20) * 30) * 40)
In this example, the function starts with a seed value of 5. It multiplies the seed by the first element of the sequence (10), resulting in 50. The subsequent steps involve multiplying the previous result (50) by the next element in the sequence (20), yielding 1000. This process continues for the remaining two elements, ultimately producing the final product of 1,200,000.
More articles
Execute a stored procedure Dapper
Learn how to use the Dapper framework to execute a stored procedure in a C#*application.*medium.com
Dapper Best Practices: C# Developers’ Guide to Database Management
*Dapper is an open-source ORM tool that is easy to use and lightweight, which makes it a popular choice for .NET…*medium.com
Using Dapper with Entity Framework
*Various tools and libraries are available to developers working with data in a C# application. Two of the most popular…*medium.com
ORM Wars: Dapper vs EF Core
The BattleContinuesmedium.com
Maximizing C# Database Performance with Dapper
*Dapper is a popular open-source micro-ORM (Object Relational Mapping) framework developed by Stack Overflow. It…*medium.com
Follow me on
C# Publication, LinkedIn, Instagram, Twitter, Dev.to