Tuesday, October 5, 2010

LINQ : the programmer's delight

In almost every computer application we come across, the need to store data. We use arrays, collections etc. to store data temporarily and also databases for a permanent storage.We also use classes and structures to model it. However inevitable operations involved with any kind of data storage is Creating , Reading, Updating, Deleting and Searching (CRUDS) data in these data stores.
Sure we can write our own code to do these but these operations occur so often that every high level language tries to integrate them in simple to use forms. And so, we have queries. Those of you who have been working with queries especially the people who have worked with SQL and Oracle must have used them.
LINQ or Language Integrated Queries are another way of querying data stores in C#.So lets find out why should one use LINQ.

A typical SQL query would look like :
The basic problems any developer comes across while writing these queries are:
  1. The query includes a string which has a specific format.
  2. You do not get any Intellisense support in Visual Studio while writing these queries.
  3. Even a small error could result in crashing of application and it could take hours to figure it out.
To solve these problems LINQ provides you a way of writing queries which is as simple as writing ordinary C# code with Intellisense in VS.
LINQ provides a set of language extensions to C# and Visual Basic and a unified programming model that extends the .NET Framework to offer integrated querying for objects, databases, and XML.
Say to search the specific words which contain the character ‘a’ from a string array, one would write a query expression as:
The variable “selected” over here is of the type ‘IEnumerable’ , a datatype which comes from System.Collections.Generic.IEnumerable Interface.
So here “selected” gets all the names having the character ‘a’ and “result” gets all the items in selected. The expressions used to denote logic (eg. n=>n) are called Lambda Expressions.
One could also order the selected items as desired and convert to results in some form, say Upper case as:
A number of such query expressions when combined together form a LINQ query. Lets see how we put the 3 queries above together in 1 LINQ query:
This was a simple example to show how a LINQ query is written. LINQ queries can be written for many types of data stores such as:
1. LINQ to Objects: LINQ for In-Memory Collections.
2. LINQ to XML: LINQ for XML Documents.
3. LINQ to DataSets: LINQ for Dataset Objects.
4. LINQ to SQL: LINQ for Connected Databases.
Lets see how would one write a similar query to fetch items from a collection of objects.
So , for a demo, I create a class ‘Customers’ which has just 2 properties: ‘Name’ and ‘City’ and Create 5 instances of it and add them to a list.


Now to write a query to select a particular object from this list we can write a LINQ query as:The ‘First’ method returns the first item of the collection.
LINQ to SQL
Finally lets see how to write a similar LINQ query against a SQL database. For this purpose we must add a “LINQ to SQL” class in the project. It has a .dbml extension
Now drag the database table we want to work with in the class designer surface.
Now to fetch any data, say name of a course in the table, we create an object of the datacontext of the ‘LINQ to SQL’ class which we created and refer the table through it in the query:

This is a lot more simple than writing an SQL query to fetch data from a database table.
Another important thing to note is that the fashion in which LINQ queries are written is the same irrespective of the data store type. So whether your data is stored in an array or a collection or an SQL database or an XML file, the LINQ query format remains almost the same, which saves the developer from remembering different types of query syntices.
That’s all for now folks. Expect another blog post in future on how to use LINQ with XML and Datasets. I hope this was helpful.

1 comment:

Manisha Kukreti said...

liked the content and the flow of your blog.You are doing a very good work here.
A sensible technical blog after a long time...keep up the good work and keep writing..