With the release of .NET framework 3.5, Microsoft introduced lots of new features. One of the very important feature introduced is LINQ, which stands for Language Structured Integrated Query. I have been studying LINQ since the last 2 months and I find it an excellent approach to data management. In this post, I will give a brief overview about it.
While writing a software, at some point of time we write code which deals with data. The data can be in any form; files stored locally on a computer, tables in a database or even XML documents. While dealing with data when we are creating applications using .NET, at times we may have to carry objects to a database and then load the results back to the object after querying the database.
The following pseudocode will explain this approach.
Students Stu = New Students(); // A class which represents a table of students
Stu.stuID = 5; // Setting the row identifier to 5
Stu.Retrieve(); // Retrieving Row’s data where Student ID = 5
Stu.Name = “Pushkar Arora”; // Changing the column ‘Name’ value to Pushkar Arora
Stu.Update(); // Updating the Database data
A real cool and simple approach, isn’t it? Now to implement this, in ADO.NET we do the following.
SqlConnection con = new SqlConnection(); // Specify the connection
// Open the connection
if (con.State == ConnectionState.Closed)
// Specify the SQL Command
SqlCommand cmd = new SqlCommand();
cmd.CommandText = “stpUpdateStudents”; // Use stored procedure for the query to select Row where @stuID = 5, from the table students.
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
cmd.Parameters.Add(“@stuID”, SqlDbType.Int).Value = 5; // Add a value to the parameter
// Excute the command
DataReader dtRd = con.Execute(cmd);
SqlDataReader dtRd = cmd.ExecuteReader();
// Retrieve the Name column value
string name = dtRd.GetString(0);
// Update record using another Command object
………Update code comes here…….
dtRd.Close(); // Close the Data Reader
Now, not only this a ‘lot’ more code than the previous simple pseudocode we had written, but also, we have the risk of having a logical error here. I mean, the compiler doesn’t check our stored procedure against the data returned. We have to know the ‘Name’ column’s position in the database table to find it in the result. We could put in the wrong value and then at run time get either type exception or bad data.
Here, LINQ plays its role. According to msdn, LINQ bridges the gap between the world of objects and the world of data. It simplifies the interaction between objects and data sources. Arrays, Collections, XML all can be used as data sources with LINQ. It provides a consistent programming model while being used against any kind of object or data source. Let’s see an example on how LINQ makes things simple. Using LINQ, to retrieve data from data source we write:
var query = from stuID in Students
where Stu.stuID = 5
That’s it! It is not a pseudocode, but the actual syntax! Maybe Microsoft is spoiling us by enabling us to write so few lines of code for something which used to be a long series of lines of code. While using LINQ we work in strongly typed environment. That is good news! We get to have our queries checked at compile time as well as have nice hints from IntelliSense feature. The C# compiler enforces type safety and logical consistency when we write data access code as part of C#, using C# keywords.
There are three major types of LINQ providers:
1. LINQ to Objects.
2. LINQ to SQL.
3. LINQ to XML.
You can know in depth about these types and more about LINQ here.
I will write more about LINQ in my subsequent posts.