What is LINQ

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

The namespace which is used for these queries is as follows:-

using System.Linq;

Example of LINQ Queries

Let’s take the string array which contains the name of student as follows:-

		

	using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //declare the array
        string[] techaltum_student = new string[] { "isha", "avinash", "guddu", "neha sharma" };
       
        //quey using LINQ syntax
        IEnumerable<string> str_res = from res in techaltum_student
                                      select res;

        //access the element and print in the screen
        foreach (string final_res in str_res)
        {
            //show data with some spaces
            Response.Write(final_res+"   ");
       
        }
    }
}
	
		
	

The output of this code as follows:-

		
		linq introduction

		Figure 1
		
		

Where Clause in LINQ

We can also filter data using where clause. Some queries and answer as follows:- Show the names which length are 4 then we will write the query as follows:-

		
		IEnumerable<string> str_res = from res in techaltum_student
									  where res.Length==4
									  select res;
	
		

Show the name which has the word “sh”

		
		IEnumerable<string> str_res = from res in techaltum_student
									  where res.Contains("sh")
									  select res;

		
		

order by in linq Query

If we want to sort the data then we use the order by in the query as follows:-

		
		IEnumerable<string> str_res = from res in techaltum_student
								      where res.Contains("sh")
									  orderby res
									  select res;

		

By default it will sort in ascending order.

If we want to sort in descending order then we will write the query as follows:-

		
		IEnumerable<string> str_res = from res in techaltum_student
									  where res.Contains("sh")
									  orderby res descending
									  select res;

		
		

Customization of linq output

We can also customize the output using query in LINQ.

For Example

If you want to show the data in upper case then we customize the output as follows:-

		
		IEnumerable<string> str_res = from res in techaltum_student
                                      where res.Contains("sh")
                                      orderby res descending
                                      select res.ToUpper();
		
		

Show Data in Gridview using linq

We can also show this data in gridview. The code is as follows:-

		
		using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //declare the array
        string[] techaltum_student = new string[] { "isha", "avinash", "guddu", "neha sharma" };
       
        //quey using LINQ syntax
        IEnumerable<string> str_res = from res in techaltum_student
  where res.Contains("sh")
  orderby res descending
  select res.ToUpper();

        GridView1.DataSource = str_res;
        GridView1.DataBind();
    }
}
		
		

How to Project Output

we can also project output in another form and can generate new results.

For Example

Let’s take the following array in which i want to show the ceiling values from the result

		
		// project elements
        decimal[] ar = { 2.5m, 3.8m, 4.9m, 5.1m, 6.4m };

        IEnumerable<decimal> res = from m in ar
                              
                               select Math.Ceiling(m);

        foreach (decimal p in res)
        {
            Response.Write(p + "<br>");

        }

		
		

It will show the following output

		
		how to project select

		
		
		

Email Address

For any query you can send mail at info@techaltum.com
Thanks