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

Friday, 14 June 2013

LINQ Hour 5-2 Sorting in LINQ

Written By:-Isha Malhotra 
Our Website:-www.techaltum.com
Sorting in LINQ

To sort data in LINQ we use orderby in the following manner:-

Sorting with Array

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

        // it will sort in ascending order
        var sort_asc = from res_asc in marks
                       orderby res_asc
                       select res_asc;

        //it will sort in descending order
        var sort_desc = from res_desc in marks
                        orderby res_desc descending
                        select res_desc;

        Response.Write("Ascending order result <br/>");
        foreach (int res_asc in sort_asc)
        {

            Response.Write(res_asc + " ");
       
        }


        Response.Write("<br/>descending order result <br/>");
        foreach (int res_desc in sort_desc)
        {

            Response.Write(res_desc + " ");

        }

The output of the code is as follows:-



Figure 1

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

Tuesday, 23 April 2013

LINQ Training - Hour 1

Written By:-Isha Malhotra(malhotra.isha3388@gmail.com)
if you find this article useful then leave your comment
LINQ-Hour 1

Language Integrated Query introduced in .net framework 3.5. LINQ integrated accessibility of data and query into language. LINQ makes easier to work with data.

Advantage of LINQ

Earlier when we connect to database, we used SQL queries as text like if we have to select any data from table then we write following queries:-

“select * from Table_name”

This query will be passed as text and if this query has some syntax error then it will be showed when this query will be executed means at runtime. But in LINQ at the time of compile time all syntax checked as it integrate query writing in our language(C# or Vb.net)

Query writing in LINQ

LINQ allows us to write query against all data whether it comes from array, database, XML etc.  This is the best feature of LINQ.

Before going deep into LINQ lets run some basic queries in LINQ using arrays and try to understand the syntax of LINQ