Loading

Microsoft .Net 6 LINQ

What is LINQ Aggregate Functions in C#?. The Complete Microsoft .Net 6 LINQ Developer Course 2022 [Videos].

In this Video, I am going to discuss the Linq Aggregate Functions in C# with examples. Please watch 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.


LINQ Aggregate Functions in C#

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.

  1. Sum(): This method is used to calculate the total(sum) value of the collection.
  2. Max(): This method is used to find the largest value in the collection
  3. Min(): This method is used to find the smallest value in the collection
  4. Average(): This method is used to calculate the average value of the numeric type of the collection.
  5. Count(): This method is used to count the number of elements present in the collection.
  6. Aggregate(): This method is used to Performs a custom aggregation operation on the values of a collection.
  7. What is Linq Sum in C#?
  8. Multiple examples using both Method and Query syntax.
  9. 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.

    using System;
    using System.Linq;
    namespace LINQDemo
    {
    class Program
    {
    static void Main(string[] args)
    {
    int[] intNumbers = new int[] { 10, 30, 50, 40, 60, 20, 70, 90, 80, 100 };
    //Using Method Syntax
    int MSTotal = intNumbers.Sum();
    //Using Query Syntax
    int QSTotal = (from num in intNumbers
    select num).Sum();
    Console.WriteLine("Sum = " + QSTotal);
    Console.ReadKey();
    }
    }
    }

    Output:

    Sum Method in C# with Examples

    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.

    using System;
    using System.Linq;
    namespace LINQDemo
    {
    class Program
    {
    static void Main(string[] args)
    {
    int[] intNumbers = new int[] { 10, 30, 50, 40, 60, 20, 70, 90, 80, 100 };
    //Using Method Syntax
    int MSTotal = intNumbers.Where(num => num > 50).Sum();
    //Using Query Syntax
    int QSTotal = (from num in intNumbers
    where num > 50
    select num).Sum();
    Console.WriteLine("Sum = " + QSTotal);
    Console.ReadKey();
    }
    }
    }

    Output:

    C# linq sum

    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.

    using System;
    using System.Linq;
    namespace LINQDemo
    {
    class Program
    {
    static void Main(string[] args)
    {
    int[] intNumbers = new int[] { 10, 30, 50, 40, 60, 20, 70, 90, 80, 100 };
    //Using Method Syntax with a Predicate
    int MSTotal = intNumbers.Sum(num => {
    if (num > 50)
    return num;
    else
    return 0;
    });
    Console.WriteLine("Sum = " + MSTotal);
    Console.ReadKey();
    }
    }
    }

    Output:

    Linq sum

    Linq Sum Method Working with Complex Type:

    We are going to work with the following Employee class.

    using System.Collections.Generic;
    namespace LINQDemo
    {
    public class Employee
    {
    public int ID { get; set; }
    public string Name { get; set; }
    public int Salary { get; set; }
    public string Department { get; set; }
    public static List<Employee> GetAllEmployees()
    {
    List<Employee> listStudents = new List<Employee>()
    {
    new Employee{ID= 101,Name = "Preety", Salary = 10000, Department = "IT"},
    new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"},
    new Employee{ID= 103,Name = "James", Salary = 50000, Department = "Sales"},
    new Employee{ID= 104,Name = "Hina", Salary = 20000, Department = "IT"},
    new Employee{ID= 105,Name = "Anurag", Salary = 30000, Department = "IT"},
    new Employee{ID= 106,Name = "Sara", Salary = 25000, Department = "IT"},
    new Employee{ID= 107,Name = "Pranaya", Salary = 35000, Department = "IT"},
    new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"},
    new Employee{ID= 109,Name = "Sam", Salary = 45000, Department = "Sales"},
    new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"}
    };
    return listStudents;
    }
    }
    }

    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.

    using System;
    using System.Linq;
    namespace LINQDemo
    {
    class Program
    {
    static void Main(string[] args)
    {
    //Using Method Syntax
    var TotalSalaryMS = Employee.GetAllEmployees()
    .Sum(emp => emp.Salary);
    //Using Query Syntax
    var TotalSalaryQS = (from emp in Employee.GetAllEmployees()
    select emp).Sum(e => e.Salary);
    Console.WriteLine("Sum Of Salary = " + TotalSalaryMS);
    Console.ReadKey();
    }
    }
    }

    Output:

    C# linq sum

    Example5:

    The following example calculates the sum of the salary of all the employees who belong to the IT department.

    using System;
    using System.Linq;
    namespace LINQDemo
    {
    class Program
    {
    static void Main(string[] args)
    {
    //Using Method Syntax
    var TotalSalaryMS = Employee.GetAllEmployees()
    .Where(emp => emp.Department == "IT")
    .Sum(emp => emp.Salary);
    //Using Query Syntax
    var TotalSalaryQS = (from emp in Employee.GetAllEmployees()
    where emp.Department == "IT"
    select emp).Sum(e => e.Salary);
    Console.WriteLine("IT Department Total Salary = " + TotalSalaryQS);
    Console.ReadKey();
    }
    }
    }

    Output:

    Linq sum in C# with examples

    Example6:

    Lets rewrite the previous example using custom predicate.

    using System;
    using System.Linq;
    namespace LINQDemo
    {
    class Program
    {
    static void Main(string[] args)
    {
    //Using Method Syntax and Predicate
    var TotalSalaryMS = Employee.GetAllEmployees()
    .Sum(emp => {
    if (emp.Department == "IT")
    return emp.Salary;
    else
    return 0;
    });
    Console.WriteLine("IT Department Total Salary = " + TotalSalaryMS);
    Console.ReadKey();
    }
    }
    }

    Output:

    C# linq sum examples

See All

Comments (470 Comments)

Submit Your Comment

See All Posts

Related Posts

Microsoft .Net 6 LINQ / Blog

What is Microsoft .Net 6 LINQ?

LINQ stands for Language-Integrated Query and it is a powerful query language that was introduced with .Net 3.5 & Visual Studio 2008. You can use LINQ with C# or VB to query different types of data sources such as SQL, XML, In memory objects, etc.
14-Feb-2022 /38 /470

Microsoft .Net 6 LINQ / Blog

What is Architecture of LINQ?

In this article, I am going to discuss the Architecture of LINQ. The term LINQ stands for Language Integrated Query and it is pronounced as LINK. Nowadays the use of use LINQ increasing rapidly. So, as a developer, you should understand the Linq and its architecture. At the end of this article, you will have a very good understanding of the following pointers.
14-Feb-2022 /38 /470

Microsoft .Net 6 LINQ / Blog

How to write Different Ways to Write LINQ Query?

In this article, I am going to discuss the Different Ways to write LINQ Query i.e. Linq Query Syntax and Linq Method Syntax with examples. Please read our previous article where we discussed the Architecture of LINQ i.e. how LINQ works. In this article, we are going to discuss the following pointers.
14-Feb-2022 /38 /470