In this Video, I am going to discuss the Linq Aggregate Functions in C# with examples. Please read our previous Video before proceeding to this Video where we discussed the Reverse Method in C# with some examples. As part of this Video, we are going to discuss the following concepts.

What are Linq Aggregate Functions in C#?
The Linq aggregate functions are used to group together the values of multiple rows as the input and then return the output as a single value. So, simple word, we can say that the aggregate function in C# is always going to return a single value.
When to use the Aggregate Functions in C#?
Whenever you want to perform some mathematical operations such as Sum, Count, Max, Min, Average, and Aggregate on the numeric property of a collection then you need to use the Linq Aggregate Functions.
What are the Aggregate Methods Provided by Linq?
The following are the aggregate methods provided by Linq to perform mathematical operations on a collection.
- Sum(): This method is used to calculate the total(sum) value of the collection.
- Max(): This method is used to find the largest value in the collection
- Min(): This method is used to find the smallest value in the collection
- Average(): This method is used to calculate the average value of the numeric type of the collection.
- Count(): This method is used to count the number of elements present in the collection.
- Aggregate(): This method is used to Performs a custom aggregation operation on the values of a collection.
- What is Linq Sum in C#?
- Multiple examples using both Method and Query syntax.
What is Linq Sum in C#?
The Linq Sum() Method belongs to the category of Aggregate Functions. The Linq Sum method in C# is used to calculates the total or sum of numeric values in the collection. Let us understand the Sum() method with some examples.
Example1:
The following example calculates the sum of all integers present in the collection.
Output:
Note: We dont have any operator called sum in Linq query syntax. So here we need to use the mixed syntax.
Example2: Linq Sum Method with filter
Now we need to calculate the sum of all numbers which is greater than 50.
Output:
Example3: Linq Sum Method with Predicate
Instead of using the where method to filter the data, you can also use the other overloaded version of the Sum method which takes a Predicate and within that predicate, you can write the logic to filter the data as shown in the below example.
Output:
Linq Sum Method Working with Complex Type:
We are going to work with the following Employee class.
This is a very simple Employee class with having four properties such as ID, Name, Salary, and Department. We also create one method i.e. GetAllEmployees() which will return the list of all the employees.
Example4:
The following example calculates the Sum of Salaries of all the employees.
Output:
Example5:
The following example calculates the sum of the salary of all the employees who belong to the IT department.
Output:
Example6:
Lets rewrite the previous example using custom predicate.
Output: