Linq's Aggregate Functions

Linq's Aggregate Functions

Aggregate functions enable you to quickly compute the average, sum, count, max, or min on a sequence. For example, if you had a list of items that represent line items on an invoice you could quickly compute the total for the invoice by using the Sum method. These functions are only available as method-based queries but can be used in a query expression. The following code samples show the query expression syntax and the equivalent method-based syntax for the aggregate functions.

Count :

int[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

 

         //Expression based query

         var total = (from i in array

                      select i).Count();

         // Method based query

         var total1 = array.Count();

 

        // output :

        // total  - 10

        // total1 - 10

Average :

int[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

 

         //Expression based query

         double average = (from i in array

                            select i).Average();

 

         // Method based query

         double average1 = array.Average();

 

        // output :

        // average  - 4.5

        // average1 - 4.5

Sum :

int[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

 

         //Expression based query

         int totalsum = (from i in array

                              select i).Sum();

 

         // Method based query

         int totalsum1 = array.Sum();

 

         // output :

         // totalsum  - 45

         // totalsum1 - 45

Min :

int[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

 

         //Expression based query

         int minvalue = (from i in array

                       select i).Min();

 

         // Method based query

         int minvalue1 = array.Min();

         // output :

         // minvalue  - 0

         // minvalue1 - 0

Max :

int[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

 

         //Expression based query

         int maxvalue = (from i in array

                       select i).Max();

 

         // Method based query

         int maxvalue1 = array.Max();

         // output :

         // maxvalue  - 9

         // maxvalue1 - 9

First :

int[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

 

         //Expression based query

         var first = (from i in array

                         select i).First();

 

         // Method based query

         var first1 = array.First();

         // output :

         // first  - 0

         // first1 - 0

Last :

int[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

 

         //Expression based query

         var last = (from i in array

                        select i).Last();

 

         // Method based query

         var last1 = array.Last();

         // output :

         // last  - 9

         // last1 - 9

Take :

You can use the Take method to limit the number of elements returned from the sequence.

int[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

 

         //Expression based query

         var take = (from i in array

                     select i).Take(2);

 

         // Method based query

         var take1 = array.Take(2);

 

         foreach (int i in take)

         {

             Response.Write(i);

         }

         foreach (int i in take1)

         {

             Response.Write(i);

         }

         // output :

         // take  - 0 1

         // take1 - 0 1

Skip :

The Skip method enables you to pass in a number and returns all elements in the sequence after that number.

int[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

 

         //Expression based query

         var skip = (from i in array

                     select i).Skip(5);

 

         // Method based query

         var skip1 = array.Skip(5);

 

         foreach (int i in skip)

         {

             Response.Write(i);

         }

         foreach (int i in skip1)

         {

             Response.Write(i);

         }

         // output :

         // skip  - 5 6 7 8 9

         // skip1 - 5 6 7 8 9

Concatenation :

The Concat method enables you to concatenate two sequences into one. This is similar to how a UNION clause works in a SQL statement.

int[] array1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

 

         int[] array2 = { 10, 11, 12, 13, 14, 15 };

 

         //Expression based query

         var concate = (from i in array1

                        select i).Concat((from s in array2

                                          select s));

 

         // Method based query

         var concate1 = array1.Concat(array2);

 

         foreach (int i in concate)

         {

             Response.Write(i);

         }

         foreach (int i in concate1)

         {

             Response.Write(i);

         }

 

         // output :

         // concate  - 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

         // concate1 - 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Distinct :

The Distinct method returns the distinct list of values in the returned sequence. This is useful when you want to remove duplicates from a sequence.

int[] array = { 1, 2, 3, 4, 1, 2, 3, 5, 6, 7, 8, 8, 9 };

 

         //Expression based query

         var distinct = (from i in array

                         select i).Distinct();

 

         // Method based query

         var distinct1 = array.Distinct();

 

         foreach (int i in distinct)

         {

             Response.Write(i);

         }

         foreach (int i in distinct1)

         {

             Response.Write(i);

         }

         // output :

        // distinct  - 1 2 3 4 5 6 7 8 9

        // distinct1 - 1 2 3 4 5 6 7 8 9