Language Integrated Query (LINQ): Part 1

Recently I had started exploring the new features of "LINQ to Entities". So I decided to start a series of articles on "LINQ" and accumulate it on my blog for beginners.


I hope this series will be helpful to you to understand the "LINQ". Before jumping in to start the application development let's talk about the basics, something like what LINQ is and what are its benefits.


What is LINQ?


In short, Language-Integrated Query (LINQ) is the name for a set of technologies based on the integration of query capabilities directly into the C# language (also in Visual Basic and potentially any other .NET language).


LINQ is a technique used to retrieve data from any object that implements the 
IEnumerable
<T> interface. In LINQ, arrays, collections, relational data, and XML are all potential data sources and we can query over it to get only those data that meets our criteria and all is done using C# and VB. In Microsoft's PDC (Professional Developers Conference) 2005, Anders Hejlsberg and his team presented the LINQ approach and its components released with the .NET 3.5 Framework.
 

Benefits of LINQ
 

Some of the major benefits of LINQ are:

  1. LINQ is integrated into the C# and VB languages and it provides syntax highlighting and IntelliSense features and even you can debug your queries using the integrated debugger in Visual Studio.
  2. Using LINQ it is possible to write code much faster than older queries and lets you save half of the time spent writing queries.
  3. Using LINQ you can easily see the relationships among tables and it helps you compose your query that joins multiple tables.
  4. Transformational features of LINQ make it easy to convert data of one type into a second type. For example, you can easily transform SQL data into XML data using LINQ.

Older Approach


To run a simple SQL query, ADO.NET programmers have to store the SQL in a Command object, associate the Command with a Connection object and execute it on that Connection object, then use a DataReader or other object to retrieve the result set. For example, the following code is necessary to retrieve the single row.

SqlConnection c =new SqlConnection(a¦);//DB Connection
c.Open(); //Open Connection
SqlCommand cmd = new SqlCommand(@"SELECT * FROM Employees e WHERE e.ID = @p0"); //SQL Query
cmd.Parameters.AddWithValue("@p0", 1); //Add value to parameter
DataReader dr = c.Execute(cmd);
//Execute the command
while (dr.Read()) {
   string name = dr.GetString(0);
}
//Get name column value

// Update record using another Command object
::::::::::::::::::::
c.Close();
//Close connection

This is not only huge code, but there's also no way for the C# compiler to check our query against our use of the data it returns. When "e" retrieves the employee's name we have to know the column's position in the database table to find it in the result. It's a common mistake to retrieve the wrong column and get a type exception or bad data at run time.
 

But ?

 

With LINQ you just need to perform three distinct actions:

  1. Obtain the data source.
  2. Create the query.
  3. Execute the query.

And you are done.


I hope you will find it useful. Thanks for reading.

Up Next
    Ebook Download
    View all
    Learn
    View all