LINQ Aggregate Functions

Aggregation functions are used to perform mathematical operations like Sum, Average, Count, Max, and Min on the elements of a collection.

An aggregate function returns a single value.

Topics

  • LINQ SUM Function
  • LINQ MIN function
  • LINQ MAX function
  • LINQ COUNT function
  • LINQ AVERAGE function
  • LINQ AGGREGATE function

LINQ SUM function

SUM function can be extremely useful to calculate the sum of the items in a list/Collection.

Sum=my_Datasource.SUM()

LINQ SUM Function Example using C#

using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqExamplesCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Int32> myNum_List = new List<Int32>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            Console.WriteLine("Calculating the sum ");
            int Sum = myNum_List.Sum();
            Console.WriteLine("Calculated Sum is {0}", Sum);
            Console.ReadLine();
        }
    }
}

Output:
Calculated Sum is 45

LINQ SUM Function Example using Vb.Net

Module Module1
    Sub Main()
        Dim myNum_list As Int32() = {1, 2, 3, 4, 5, 6, 7, 8, 9}
        Console.WriteLine("Calculating the sum ") 
        Dim Sum As Int32 = myNum_list.Sum()
        Console.WriteLine("Calculated Sum is {0}", Sum)
        Console.ReadLine()
    End Sub
End Module

Output:
Calculated Sum is 45

LINQ MIN function

MIN function can be used to find the minimum value in a list/Collection.

Min=my_Datasource.MIN()

LINQ MIN Function Example using C#

using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqExamplesCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Int32> myNum_List = new List<Int32>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            Console.WriteLine("Finding the minimum value ");
            int Min = myNum_List.Min();
            Console.WriteLine("Minimum value is {0}", Min);
            Console.ReadLine();
        }
    }
}
Output:
Minimum value is 1

LINQ MIN Function Example using Vb.Net

Module Module1
  Sub Main()
    Dim myNum_list As Int32() = {1, 2, 3, 4, 5, 6, 7, 8, 9}
    Console.WriteLine("Finding the minimum value ")
    Dim Min As Int32 = myNum_list.Min()
    Console.WriteLine("Minimum value is {0}", Min)
    Console.ReadLine()
  End Sub
End Module

Output:
Minimum value is 1

LINQ MAX function

MAX function can be used to find the maximum value in a list/Collection.

Max=my_Datasource.MAX()

LINQ MAX Function Example using C#

using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqExamplesCSharp
{
 class Program
 {
 static void Main(string[] args)
 {
 List<Int32> myNum_List = new List<Int32>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 Console.WriteLine("Finding the maximum value ");
 int Max = myNum_List.Max();
 Console.WriteLine("Maximum value is {0}", Max);
 Console.ReadLine();
 }
 }
}
Output:
Maximum value is 9

LINQ MAX Function Example using Vb.Net

Module Module1
 Sub Main()
    Dim myNum_list As Int32() = {1, 2, 3, 4, 5, 6, 7, 8, 9}
    Console.WriteLine("Finding the maximum value ")
    Dim Max As Int32 = myNum_list.Max()
    Console.WriteLine("Maximum value is {0}", Min)
    Console.ReadLine()
 End Sub
End Module

Output:
Maximum value is 9

LINQ COUNT function

COUNT function can be used to count the number of items in a list/Collection.

Count=my_Datasource.COUNT()

LINQ COUNT Function Example using C#

using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqExamplesCSharp
{
class Program
{
static void Main(string[] args)
{
List<Int32> myNum_List = new List<Int32>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Console.WriteLine("Finding the count");
int Count = myNum_List.Count();
Console.WriteLine("Count is {0}", Count);
Console.ReadLine();
}
}
}

Output:
Count is 1

LINQ COUNT Function Example using Vb.Net

Module Module1
 Sub Main()
    Dim myNum_list As Int32() = {1, 2, 3, 4, 5, 6, 7, 8, 9}
    Console.WriteLine("Finding the count")
    Dim Count As Int32 = myNum_list.Count()
    Console.WriteLine("Count is {0}", Count)
    Console.ReadLine()
 End Sub
End Module

Output:
Count is 1

LINQ AVERAGE function

The AVERAGE function can be used to find the average value of the items in a list/Collection.

Sum=my_Datasource.AVERAGE()

LINQ AVERAGE Function Example using C#

using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqExamplesCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Int32> myNum_List = new List<Int32>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            Console.WriteLine("Finding the average value ");
            Double Avg = myNum_List.Average();
            Console.WriteLine("Average value is {0}", Avg);
            Console.ReadLine();
        }
    }
}
Output:
Average value is 1

LINQ MIN Function Example using Vb.Net

Module Module1
  Sub Main()
    Dim myNum_list As Int32() = {1, 2, 3, 4, 5, 6, 7, 8, 9}
    Console.WriteLine("Finding the average value ")
    Dim Avg As Double = myNum_list.Average()
    Console.WriteLine("Average value is {0}", Avg)
    Console.ReadLine()
  End Sub
End Module

Output:
Average value is 1

Previous topic: LINQ Lambda Expressions
Next Topic: LINQ Filtering Operators

For more articles on LINQ, Click here

Advertisements

1 thought on “LINQ Aggregate Functions

  1. […] topic: LINQ Aggregate Functions Next Topic: Posting […]

Leave a Reply

%d bloggers like this: