👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
Using LINQ Count Min Max Average and Sum to Aggregate data

Using LINQ Count Min Max Average and Sum to Aggregate data

LINQ

26 Articles

Improve

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

  1. Introduction
  2. Count()
  3. Min()
  4. Max()
  5. MinBy()
  6. MaxBy()
  7. Average()
  8. Sum()
  9. Aggregate()
  10. Summary

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 Space

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 Space

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 Space

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 Space

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 Space

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 Space

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 Space

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 Space

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.

👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
  • LINQ
  • Count
  • Min
  • Max
  • MinBy
  • MaxBy
  • Average
  • Sum
  • Aggregate