Showing posts with label queries with linq. Show all posts
Showing posts with label queries with linq. Show all posts

Friday, 14 June 2013

LINQ Hour 5 -1 Aggregate function in LINQ

Written By:-Isha Malhotra 
Our Website:-www.techaltum.com
Aggregate function using LINQ

We use many aggregate functions like sum, count, average, min, and max. In LINQ we can use this function in the following manner:-

SUM

If we want to perform sum aggregate function then we will implement the following queries:-

Sum with array

int[] marks = new int[] { 23, 45, 78, 90, 56, 89, 10, 32 };

        //simple sum without condition

        int res = marks.Sum();

        //sum with condition
        int res_with_where = marks.Where(cond => cond > 50).Sum();

      

        Response.Write("Sum without condition= " + res + "<br/>");

        Response.Write("Sum with condition="+ res_with_where+"<br/>");

Output of this code as follows:-



Figure 1