Using LINQ Count Min Max Average and Sum to Aggregate data
LINQ
26 Articles
In this article, let's learn about how to use Count
, Min
, Max
,
MinBy
, MaxBy
, Average
, Sum
and Aggregate
in LINQ
in .NET.
Note: If you have not done so already, I recommend you read the article on Using LINQ Group By to group data.
Table of Contents
Introduction
When working with group of data we often need to do calculate some aggregate value out of it. With LINQ, the aggregate functions such as Count
,
Min
, Max
, MinBy
, MaxBy
,
Average
, Sum
and Aggregate
are used to calculate a single value
from a property in collection of objects. The Aggregate method is always applied after the query executes, which means it's already gotten the whole collection by applying
all the where clauses and everything, it retrieves the list, and then the method is applied.
LINQ Aggregate
functions is used to answer questions about collection such as
- Finding the average sales happened during the season
- Validating the count of records
- Finding the total products value in a store
- Minimum / Maxmimum tempertaure recorded in a day
Count()
The Count()
function returns the number of elements in a collection. This is a handy way to quickly determine the size of a collection,
and can be useful in a variety of scenarios, such as verifying that a list contains a minimum number of items before processing it further.
Code Sample - LINQ Count Clause
Demo - LINQ Count Demo
Let's try LINQ Count with Objects
- We have
Product
class with following properties -Id, Name, Color, Price, Size
. - The tables is loaded with product collection.
- Click on Count Button to view the result.
- Click on reset to try other combination
Id | Name | Color | Price | Size |
---|---|---|---|---|
1 | Shirt | Black | 1000 | 18 |
2 | Shirt | Red | 1500 | 28 |
3 | Shirt | Black | 2000 | 38 |
4 | Shirt | Red | 2500 | 48 |
5 | Shirt | Brown | 3000 | 58 |
6 | Shirt | White | 3500 | 68 |
Count:
Min()
The Min()
function return the minimum value from a collection of numbers. For example, you can use Min to find the smallest number in
a list of prices.
Code Sample - LINQ Min Clause
Demo - LINQ Min Demo
Let's try LINQ Min with Objects
- We have
Product
class with following properties -Id, Name, Color, Price, Size
. - The tables is loaded with product collection.
- The key expression here is
Price
. - Click on Min Button to view the result.
- Click on reset to try other combination
Id | Name | Color | Price | Size |
---|---|---|---|---|
1 | Shirt | Black | 1000 | 18 |
2 | Shirt | Red | 1500 | 28 |
3 | Shirt | Black | 2000 | 38 |
4 | Shirt | Red | 2500 | 48 |
5 | Shirt | Brown | 3000 | 58 |
6 | Shirt | White | 3500 | 68 |
Min:
Max()
The Max()
function return the maximum value from a collection of numbers. For example, you can use Max to find the largest number in
a list of heights.
Code Sample - LINQ Max Clause
Demo - LINQ Max Demo
Let's try LINQ Max with Objects
- We have
Product
class with following properties -Id, Name, Color, Price, Size
. - The tables is loaded with product collection.
- The key expression here is
Price
. - Click on Max Button to view the result.
- Click on reset to try other combination
Id | Name | Color | Price | Size |
---|---|---|---|---|
1 | Shirt | Black | 1000 | 18 |
2 | Shirt | Red | 1500 | 28 |
3 | Shirt | Black | 2000 | 38 |
4 | Shirt | Red | 2500 | 48 |
5 | Shirt | Brown | 3000 | 58 |
6 | Shirt | White | 3500 | 68 |
Max:
MinBy()
The MinBy()
function is similar to Min, but instead of finding the minimum value in a collection of numbers, they find the minimum value
based on a specific property of the objects in the collection. For example, you could use MinBy to find the employee with the lowest salary.
Code Sample - LINQ MinBy Clause
Demo - LINQ MinBy Demo
Let's try LINQ MinBy with Objects
- We have
Product
class with following properties -Id, Name, Color, Price, Size
. - The tables is loaded with product collection.
- The key expression here is
Price
. - Click on MinBy Button to view the result.
- Click on reset to try other combination
Id | Name | Color | Price | Size |
---|---|---|---|---|
1 | Shirt | Black | 1000 | 18 |
2 | Shirt | Red | 1500 | 28 |
3 | Shirt | Black | 2000 | 38 |
4 | Shirt | Red | 2500 | 48 |
5 | Shirt | Brown | 3000 | 58 |
6 | Shirt | White | 3500 | 68 |
MaxBy()
The MaxBy()
function is similar to Max, but instead of finding the maximum value in a collection of numbers, they find the maximum value
based on a specific property of the objects in the collection. For example, you could use MaxBy to find the student with the highest grade.
Code Sample - LINQ MaxBy Clause
Demo - LINQ MaxBy Demo
Let's try LINQ MaxBy with Objects
- We have
Product
class with following properties -Id, Name, Color, Price, Size
. - The tables is loaded with product collection.
- The key expression here is
Price
. - Click on MaxBy Button to view the result.
- Click on reset to try other combination
Id | Name | Color | Price | Size |
---|---|---|---|---|
1 | Shirt | Black | 1000 | 18 |
2 | Shirt | Red | 1500 | 28 |
3 | Shirt | Black | 2000 | 38 |
4 | Shirt | Red | 2500 | 48 |
5 | Shirt | Brown | 3000 | 58 |
6 | Shirt | White | 3500 | 68 |
Average()
The Average()
function returns the average value of a collection of numbers. This can be useful for determining the mean value in a
collection of data, such as the average height of a group of people.
Code Sample - LINQ Average Clause
Demo - LINQ Average Demo
Let's try LINQ Average with Objects
- We have
Product
class with following properties -Id, Name, Color, Price, Size
. - The tables is loaded with product collection.
- The key expression here is
Price
. - Click on Average Button to view the result.
- Click on reset to try other combination
Id | Name | Color | Price | Size |
---|---|---|---|---|
1 | Shirt | Black | 1000 | 18 |
2 | Shirt | Red | 1500 | 28 |
3 | Shirt | Black | 2000 | 38 |
4 | Shirt | Red | 2500 | 48 |
5 | Shirt | Brown | 3000 | 58 |
6 | Shirt | White | 3500 | 68 |
Average:
Sum()
The Sum()
function returns the total sum of a collection of numbers. This can be useful for computing the total cost of a group of items,
for example, compute the sum of total sale happened in a day.
Code Sample - LINQ Sum Clause
Demo - LINQ Sum Demo
Let's try LINQ Sum with Objects
- We have
Product
class with following properties -Id, Name, Color, Price, Size
. - The tables is loaded with product collection.
- The key expression here is
Price
. - Click on Sum Button to view the result.
- Click on reset to try other combination
Id | Name | Color | Price | Size |
---|---|---|---|---|
1 | Shirt | Black | 1000 | 18 |
2 | Shirt | Red | 1500 | 28 |
3 | Shirt | Black | 2000 | 38 |
4 | Shirt | Red | 2500 | 48 |
5 | Shirt | Brown | 3000 | 58 |
6 | Shirt | White | 3500 | 68 |
Sum:
Aggregate()
The Aggregate()
function is the most flexible of the aggregate functions in LINQ, allowing you to perform custom aggregations on a
collection of data. For example, you could use Aggregate to find the product of all the numbers in a collection, or to concatenate all the strings in a list into a single
string.
Code Sample - LINQ Aggregate Clause
Demo - LINQ Aggregate Demo
Let's try LINQ Aggregate with Objects
- We have
Product
class with following properties -Id, Name, Color, Price, Quantity
. - The tables is loaded with product collection.
- The key expression here is
Price, Quantity
. - Click on Aggregate Button to view the result.
- Click on reset to try other combination
Id | Name | Color | Price | Quantity |
---|---|---|---|---|
1 | Shirt | Black | 1000 | 1 |
2 | Shirt | Red | 1500 | 2 |
3 | Shirt | Black | 2000 | 3 |
4 | Shirt | Red | 2500 | 4 |
5 | Shirt | Brown | 3000 | 5 |
6 | Shirt | White | 3500 | 6 |
Aggregate:
Summary
In this article we learn't how to use aggregate functions on group of data using Count
, Min
,
Max
, MinBy
, MaxBy
, Average
,
Sum
and Aggregate
. LINQ aggregate functions are a valuable tool for working with collections of data in C#.
Whether you're counting items, finding the minimum or maximum value, computing the average or sum, or even performing custom aggregations, LINQ makes it simple and efficient to
perform these operations. All these can be used with any IEnumerable
or IQueryable
types.