# Aggregate Operators In LINQ

**In this article we will discuss about the Aggregate Operators in LINQ.
The Aggregate Operators in Linq allow us to perform simple math operations over the elements in a sequence.
There are mainly six types of Set Operators in Linq.**

## Aggregate Operators in Linq

### Introduction

In this article we will discuss about the Aggregate Operators in LINQ. The Aggregate Operators in Linq allow us to perform simple math operations over the elements in a sequence.

There are mainly six types of Set Operators in Linq.

1. Count - Count the elements in a sequence.

2. LongCount - Count the elements in a very, very long sequence.

3. Sum - Add the elements in a sequence

4. Min - Find the smallest element in a sequence.

5. Max - Find the largest element in a sequence.

6. Average - Find the average value in a sequence.

Now lets discuss all of them one by one.

**1. Count**

public void CountFunction()

{

int[] seq1 = { 1, 2, 3, 5, 4, 5, 2, 1 };

var count1 = seq1.Count();

Console.WriteLine("Count of seq1 is : " + count1);

var count2 = "pa55w0rd".Count (c => char.IsDigit (c));

Console.WriteLine("Count with predicate is : " + count2)

}

Output

Count of seq1 is : 8

Count with predicate is : 3

The Count operator return the number of elements in a sequence. The return value is an integer.

**2. LongCount**

public void LongCountFunction()

{

int[] seq1 = { 258797878, 25447855, 14478454 };

var longCount = seq1.LongCount();

Console.WriteLine("Count of seq1 is : " + longCount);

}

Output

Count of seq1 is : 3

The LongCount operator provides the same basic functionality as of Count, but allows you to work with an Int64. It Count the elements in a very, very long sequence. The key difference is that the result is a long, rather than a standard integer.

**3. Sum**

public void SumFunction()

{

int[] seq1 = { 1, 2, 3 };

var sum = seq1.Sum();

Console.WriteLine("Summation of the elements in array is : " + sum);

string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };

sum = names.Sum (s => s.Length);

Console.WriteLine("Summation of the elements in string array is : " + sum);

}

Output

Summation of the elements in array is : 6

Summation of the elements in string array is : 19

The Sum method totals the values in a sequence. When used with a collection that contains only numeric values like in a "seq1", the method can be used with no arguments but if you are working with non numeric types like in "names" then the overload function comes into play which accepts a single parameter.

**4. Min**

public void MinFunction()

{

int[] seq1 = { 5, 2, 3 };

var minValues = seq1.Min();

Console.WriteLine("Minimum of an array is : ");

}

Output

Minimum of an array is : 2

The Min() Function will find the smallest element in a sequence.

**5. Max**

public void MaxFunction()

{

int[] seq1 = { 3, 2, 5 };

var maxValues = seq1.Max();

Console.WriteLine("Maximum of an array is : ");

}

Output

Maximum of an array is : 5

The Max() Function will find the largest element in a sequence.

**6. Average**

public void IntersectFunction()

{

int[] seq1 = { 1, 2, 3 };

var average = seq1.Average();

Console.WriteLine("Average of seq1 is : " + average);

}

Output

Average of seq1 is : 2

This operator finds the mean of the values in a sequence or we can say that it finds the average value in a sequence.

### Feedback

If you have any queries regarding Conversion Operators, please do ask me.

GoldPoints : 1