C# > Advanced C# > LINQ > Aggregation (sum, avg, count)
LINQ Aggregation: Sum, Average, and Count Operations
This example demonstrates how to use LINQ to perform aggregation operations like calculating the sum, average, and count of elements in a collection. These are fundamental operations when you need to analyze numerical data.
Basic Example: Sum, Average, and Count
This code snippet shows the basic usage of `Sum()`, `Average()`, and `Count()` methods in LINQ. The `Sum()` method calculates the sum of all elements in the `numbers` list. The `Average()` method calculates the average value. The `Count()` method returns the total number of elements in the collection. The `Count(predicate)` overload demonstrates filtering the collection based on a condition before counting.
using System;
using System.Collections.Generic;
using System.Linq;
public class LinqAggregationExample
{
public static void Main(string[] args)
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Calculate the sum
int sum = numbers.Sum();
Console.WriteLine($"Sum: {sum}");
// Calculate the average
double average = numbers.Average();
Console.WriteLine($"Average: {average}");
// Calculate the count
int count = numbers.Count();
Console.WriteLine($"Count: {count}");
// Calculate count using LINQ
int linqCount = numbers.Count(n => n > 5); // Counts numbers greater than 5
Console.WriteLine($"Count (numbers > 5): {linqCount}");
}
}
Explanation of Concepts
LINQ (Language Integrated Query) provides a powerful and concise way to query and manipulate data. Aggregation functions like `Sum`, `Average`, and `Count` are used to perform calculations on collections of data. These functions are extension methods defined in the `System.Linq` namespace, allowing them to be called directly on any `IEnumerable
Real-Life Use Case
Imagine you have a list of sales transactions. You can use `Sum()` to calculate the total revenue, `Average()` to find the average transaction value, and `Count()` to determine the total number of transactions. Furthermore, `Count(predicate)` can be used to count the number of transactions exceeding a certain amount, useful for identifying high-value customers or analyzing trends.
Best Practices
Interview Tip
Be prepared to discuss the differences between `Sum`, `Average`, and `Count`. Understand how to use predicates with `Count` for conditional counting. Be prepared to explain how these methods work under the hood (as extension methods using `IEnumerable
When to Use Them
Use these aggregation methods when you need to summarize data from a collection. They are particularly useful when dealing with numerical data, but `Count()` can be used with any type of collection to determine the number of elements. Consider using them in data analysis, reporting, and business logic calculations.
Memory Footprint
These aggregation methods generally have a small memory footprint, as they typically operate on the collection sequentially without needing to store the entire collection in memory. The main memory usage comes from the collection itself and any intermediate variables used in the calculation. For very large datasets, consider using streaming aggregation techniques to minimize memory usage further.
Alternatives
Pros
Cons
FAQ
-
What happens if the collection is empty when calling Average()?
If the collection is empty, `Average()` will return 0 for numeric types. If you're using `Nullable` then `null` will be returned. -
Can I use Sum() on a collection of strings?
No, `Sum()` is designed for numeric types. You can sum the lengths of strings, or parse the strings to numbers first. -
How does Count(predicate) work?
The `Count(predicate)` method takes a lambda expression (a function) as a parameter. This lambda expression is evaluated for each element in the collection, and the method counts only the elements for which the lambda expression returns `true`.