Today, we will learn about Linq aggregate functions. Linq provides various aggregate functions like Min(), Max(), Sum(), Count(), Average(), Aggregate().

Linq Min() Function

Here, we will learn about Min() function provided by Linq. As the name suggests, the Min() function gives the minimum value from the list or collection. Linq made it very easy to find out min value from the list as earlier we have to write a bit of coding for finding the min value.

Linq MIN() Function Syntax

int[] numbers = { 50,25,12,63,45,98,63,52,47,52 };
int min = numbers.Min();

As we can see we have used the Min() function for it and it will give us the min value from the list we have provided.

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinqDemoes
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = { 50,25,12,63,45,98,63,52,47,52 };
            int min = numbers.Min();
            Console.WriteLine("The Minimum Number is {0}", min);
            Console.ReadLine();
        }
    }
}

In this example, we have made an array and stored it in numbers array and find the min value using the Min() function of Linq.

Output:

The Minimum Number is 12

Linq Max() Function

Here, we will learn about Max() function provided by Linq. As the name suggests, the Max() function gives the maximum value from the list or collection. Linq made it very easy to find out max value from the list as earlier we have to write a bit of coding for finding the max value.

Linq MAX() Function Syntax

int[] numbers = { 50,25,12,63,45,98,63,52,47,52 };
int max = numbers.Max();

As we can see we have used the Max() function for it and it will give us the max value from the list we have provided.

Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinqDemoes
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = { 50,25,12,63,45,98,63,52,47,52 };
            int max = numbers.Max();
            Console.WriteLine("The Maximum Number is {0}", max);
            Console.ReadLine();
        }
    }
}

In this example, we have made an array and stored it in numbers array and find the max value using the Max() function of Linq.

Output:

The Maximum Number is 98

Linq Sum() Function

Here, we will learn about the Sum() function provided by Linq. As the name suggests, the Sum() function sums up the values from the list or collection. Linq made it very easy to sum the values of the list as earlier we have to write a loop for making the sum.

Linq Sum Function Syntax

int[] numbers = { 50,25,12,63,45,98,63,52,47,52 };
int sum = numbers.Sum();

Example

Here, we will sum all the numbers of the array using the Linq sum function

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinqDemoes
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = { 50,25,12,63,45,98,63,52,47,52 };
            int sum = numbers.Sum();
            Console.WriteLine("Sum is {0}", sum);
            Console.ReadLine();
        }
    }
}

Output:

Sum is 507

Linq Count() Function

Here, we will learn about the Count() function provided by Linq. As the name suggests, the Count() function counts the number of items from the list or array

Linq Count Function Syntax

int[] numbers = { 50,25,12,63,45,98,63,52,47,52 };
int count = numbers.Count();

Example

Here, we will count all the numbers of the array using the Linq count function

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinqDemoes
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = { 50,25,12,63,45,98,63,52,47,52 };
            int count = numbers.Count();
            Console.WriteLine("Number of items are {0}", count);
            Console.ReadLine();
        }
    }
}

Output:

Number of items are 10

Linq Average() Function

Here, we will learn about the Average() function provided by Linq. As the name suggests, the Average() function finds the average of the number of items in the list or collection

Linq Average Function Syntax

int[] numbers = { 50,25,12,63,45,98,63,52,47,52 };
int average = numbers.Average();

Example

Here, we will find the average of all the numbers of the array using the Linq average function

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinqDemoes
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = { 50,25,12,63,45,98,63,52,47,52 };
            double average = numbers.Average();
            Console.WriteLine("Average is {0}", average);
            Console.ReadLine();
        }
    }
}

Output:

Average is 50.7