c sharp

LINQ Aggregate Functions in C#

The LINQ Aggregate() function is used to perform the cumulative operation on a sequence of elements. When used, the Aggregate() method returns a single value. For many numerical calculations, the LINQ Aggregate() functions include “Sum”, “Count”, “Max”, “Min”, “Average”, and “Aggregate” methods that are widely used to conduct mathematical operations.

In this guide, we will illustrate the LINQ Aggregate() functions in C# and also discuss its different methods and implement it in a single program.

What are LINQ Aggregate() Functions in C#?

In C#, LINQ Aggregate() functions are a collection of methods that enable users to carry out calculations in multiple items while returning a single result. The syntax of the LINQ Aggregate() function in C# is provided below:

var result = collection.Aggregate(function);

In the above syntax, collection refers to the series of data that is being processed. While function is a parameter passed to the “Aggregate()” function that refers to the mathematical operation being done. The outcome will be saved into the “result” variable.

Usage of LINQ Aggregate Methods in C#

LINQ “Aggregate()” functions perform mathematical operations. As follows:

Aggregate() Method

The “Aggregation()” function is applied to all the elements of the source sequence and calculates a collection of values that considers the return value of each function call. The syntax of the LINQ Aggregate() function is mentioned below:

double result = array.Aggregate((n1,n2) => n1*n2);

Here, the “Aggregate()” function used lambda expression (n1, n2) => n1 * n2 where (n1, n2) are the parameters and n1*n2 is the operation to be done on the parameters.

Average() Method

It computes the collection’s average for the numeric elements. The syntax for LINQ aggregate function Average() is stated below:

int result = array.Average();

Count() Method

It counts all the number of elements in the source sequence, just those that meet a predicate function. As follows:

int result = array.Count();

In the above syntax, an array is the collection of items and the value of the total number of elements of the array will be stored in the result variable.

Max() Method

It determines the collection’s largest value from the array. The general syntax for LINQ aggregate function “Max()” is described below:

int my_result = array.Max();

In the above syntax, the array is the combination of elements, and the “Max()” method will retrieve the maximum value among the elements of the array and then store it in the “my_result” variable.

Min() Method

It is used to retrieve the collection of the smallest values:

int result1 = array.Min();

Sum() Method

The “Sum()” method is used to find the sum of all the given values of the array:

int result2 = array.Sum();

Example: Implementation of LINQ Aggregate Functions

The LINQ aggregate functions are used in the below-provided code. Here, we are calculating the total number of elements, sum, minimum value, maximum value, product(with the help of aggregate function), and the average of the provided values of an array:

using System;

using System.Linq;

public class Sum {

static public void Main() //main method

{

int[] array1 = {5, 10, 50, 68, 90, 100, 99, 4, 57, 49};

// finding total elements of the array1

Console.WriteLine("The total elements of the array1 are= ");

int result6 = array1.Count();

Console.WriteLine("\n"+result6);

Console.WriteLine("The sum of the array1= ");

// Finding sum of the given array

int result1 = array1.Sum();

Console.WriteLine("\n"+result1);

Console.WriteLine("The maximum of value of given array1=");

// Finding the maximum value of the given array1

int result2 = array1.Max();

Console.WriteLine("\n"+result2);

// Finding the minimum value of the given array1

Console.WriteLine("The minimum value of array1= ");

int result3 = array1.Min();

Console.WriteLine(result3);

// Finding the average of all the values of the given array1

Console.WriteLine("The average value of given array1= ");

double result4 = array1.Average();

Console.WriteLine(result4);

// Finding product of the given array1 using aggregate function

Console.WriteLine("The product of array1= ");

double result5 = array1.Aggregate((n1,n2 ) => n1*n2);

Console.WriteLine(result5);

}

}

Output

That’s all! We have described the LINQ aggregate functions in C#.

Conclusion

In C#, LINQ aggregate functions are used for performing mathematical operations on data sequences. Developers may acquire important insights into their data by using different techniques, such as “Sum()”, “Min()”, “Max()”, “Average()”, and “Count()”. In this tutorial, we have illustrated the LINQ aggregate functions in C#.

About the author

Kaynat Asif

My passion to research new technologies has brought me here to write for the LinuxHint. My major focus is to write in C, C++, and other Computer Science related fields. My aim is to share my knowledge with other people.