LINQ (Language Integrated Query) - Part 1

Recently I had started exploring ‘LINQ to Entities’ new features. So, I thought to start a series of posts on ‘LINQ’ title and accumulate it on my blog for the beginner’s.

Hope, this series will be helpful to you to understand the ‘LINQ’ and before jump start to the application development let’s talk on basics something like: what is linq and 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 LINQ approach and its component released with .NET 3.5 Framework.

Benefits of LINQ

Some of the major benefits of LINQ:
  1. LINQ is integrated into C# and VB languages and it provides syntax highlighting and IntelliSense features and even you can debug your queries using integrated debugger in Visual Studio.
  2. Using LINQ it is possible to write codes much faster than older queries and lets you save half of the query writing time.
  3. Using LINQ you can easily see the relationships between 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(…); //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’ retrieve 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 done.

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


Post a Comment

Popular posts from this blog

Migrating database from ASP.NET Identity to ASP.NET Core Identity

Customize User's Profile in ASP.NET Identity System

Lambda two tables and three tables inner join code samples