Aggregation functions in LINQ are used to perform calculations on a collection of values and return a single result. These functions are commonly used to calculate totals, averages, counts, minimum values, and maximum values from collections such as arrays, lists, and database records.
- LINQ provides aggregation methods such as Sum(), Average(), Count(), Min(), Max(), and Aggregate().
- These methods simplify data calculations without requiring developers to write explicit loops.

Example: Program to calculates the sum of an integer array using the LINQ Sum() method.
using System;
using System.Linq;
class Geeks{
static void Main(){
int[] numbers = { 10, 20, 30, 40, 50 };
// Calculate the sum of array elements
int sum = numbers.Sum();
Console.WriteLine("Sum = " + sum);
}
}
Output
Sum = 150
Explanation: The Sum() method adds all the elements of the numbers array and returns their total, which is 150.
Aggregation Methods in LINQ
LINQ provides several methods for performing aggregation operations on collections.
1. Sum() Method in LINQ
The Sum() method calculates the total of numeric elements in a collection. It is commonly used to calculate total marks, sales amounts, expenses, or other numerical values.
- Adds all numeric elements in a collection.
- Can calculate the sum of a numeric property using a selector.
Syntax:
collection.Sum();
Example: Sum of Array Elements Using LINQ
using System;
using System.Linq;
class Geeks{
static void Main(){
int[] numbers = { 20, 40, 50, 68, 90, 89, 99, 9, 57, 69 };
// Find the sum of all array elements
int result = numbers.Sum();
Console.WriteLine("Sum of array elements: " + result);
}
}
Output
Sum of array elements: 591
Explanation: The Sum() method adds all 10 elements of the array and returns their total, 591, without using an explicit loop.
2. Count() and LongCount() Operators
Both operators count elements in a sequence.
- Count() returns the number of elements as an int.
- LongCount() returns the number of elements as a long, supporting counts larger than the range of int.
Syntax:
collection.Count();
collection.LongCount();
Example: Program to demonstrate Count() and LongCount() operators in LINQ.
using System;
using System.Linq;
class Geeks{
static void Main()
{
int[] numbers = { 10, 20, 30, 40, 50 };
// Count all elements
int count = numbers.Count();
// Count elements greater than 20
long filteredCount = numbers.LongCount(x => x > 20);
Console.WriteLine("Count: " + count);
Console.WriteLine("LongCount: " + filteredCount);
}
}
Output
Count: 5 LongCount: 3
Explanation: Count() returns the total number of elements, while LongCount() counts elements greater than 20.
3. Aggregate() Operator
The Aggregate() method performs a custom operation on the elements of a collection and combines them into a single result. It is useful when predefined methods such as Sum() or Max() do not meet a specific requirement.
- Applies a custom accumulator function to combine elements.
- Supports operations such as multiplication and custom calculations.
Syntax:
collection.Aggregate((a, b) => operation);
Example: Program to demonstrate calculating the product of array elements using Aggregate().
using System;
using System.Linq;
class Geeks{
static void Main()
{
int[] numbers = { 2, 3, 4 };
// Multiply elements using Aggregate()
int product = numbers.Aggregate((a, b) => a * b);
Console.WriteLine("Product: " + product);
}
}
Output
Product: 24
Explanation: Aggregate() multiplies the elements sequentially: 2 × 3 × 4 = 24.
4. Min() and Max() Methods in LINQ
The Min() and Max() methods find the smallest and largest values in a collection, respectively.
- Min() returns the minimum value.
- Max() returns the maximum value.
Syntax:
collection.Min();
collection.Max();
using System;
using System.Linq;
class Geeks{
static void Main(){
int[] numbers = { 201, 39, 50, 9, 7, 99 };
// Find minimum and maximum elements
int minimum = numbers.Min();
int maximum = numbers.Max();
Console.WriteLine("Minimum Value = " + minimum);
Console.WriteLine("Maximum Value = " + maximum);
}
}
Output
Minimum Value = 7 Maximum Value = 201
Explanation: Min() finds the smallest value (7), while Max() finds the largest value (201) in the array.
Uses of Aggregation Functions in LINQ
Aggregation functions are commonly used in applications where multiple data values need to be summarized into a single meaningful result.
- Sales Analysis: Calculate total revenue using Sum() and average sales using Average().
- Student Management: Find the highest and lowest marks using Max() and Min().
- Inventory Management: Count available products or products meeting a specific condition using Count().
- Employee Management: Calculate average salaries and total payroll expenses.
- Data Processing: Perform custom calculations or combine values using Aggregate().
Advantages of Aggregation Functions in LINQ
- Reduce the need to write explicit loops for common calculations.
- Make code shorter, easier to read, and maintain.
- Support arrays, lists, and other LINQ-compatible data sources.
- Allow calculations on selected properties and filtered collections.
- Provide built-in methods for common calculations and flexibility for custom aggregations.
Important Points About LINQ Aggregation Functions
- Import the System.Linq namespace to use LINQ aggregation extension methods.
- Aggregation methods typically produce a single result and execute when called.
- Sum() and Count() return zero for empty numeric or object collections, respectively.
- Average(), Min(), Max(), and Aggregate() can throw InvalidOperationException when applied to an empty collection, depending on their overloads and element types.
Comparison of LINQ Aggregation Operators
Operator | Description |
|---|---|
Calculates the sum of numeric values. | |
Calculates the arithmetic mean of numeric values. | |
Returns the minimum value in a sequence. | |
Returns the maximum value in a sequence. | |
Counts elements and returns an int. | |
LongCount() | Counts elements and returns a long. |
Aggregate() | Performs a custom accumulation operation. |