There's something out there in life that will delight and amaze you.
Get up, get moving, and go find out what it is. - (Ralph Marston)

Monday, May 12, 2008

LINQ in Brief

What is LINQ?

LINQ stands for Language INtegrated Query. Basically this presents a mechanism for retrieving, sorting, manipulating data which are in various sources. As LINQ is new to .NET so as the syntax.

List<int> Prices = new List<int> {456,789,235,723 };

var myPriceList = from p in Prices orderby p descending select p

In the above line of code, from the Prices List<int>, p is specified as a representation of an item in the collection, saying to get p which are in Prices List<int> based on the ordering clause and finally selecting the item.

Difference between LINQ & SQL, .NET 3.5

Seeing LINQ in .NET 3.5 suddenly made me guess whether it operates like SQL too? Linq uses similar syntax as in SQL but its functionality is totally different to SQL.

SQL basically operates on tables, while LINQ could be operated on collections, and other datasources such as databases or xml. Unlike dealing with rows in a table, these collections could be manipulated using LINQ, whether they have values in them or even objects. Let’s look at some interesting features in LINQ.

Some Interesting Features in LINQ

A collection could be modified retaining the initial values of the collection as it is. For an example, let’s say you retrieve prices and you want to add the currency symbol to the prices. In spite of LINQ, if we were to retrieve the values and add the currency symbol to it, may require traversing through the collection. But LINQ does it simply as this.

private static void GetPriceList()


List<int> Prices = new List<int> {456,789,235,723 };

var myPriceList = from p in Prices orderby p descending select p.ToString("C");

foreach (string i in myPriceList)





Not only that, calculations too could be performed on them. Let’s say in the above list you want to get the average price. Here is how to get it.

Console.WriteLine("The average is {0:F2}", Prices.Average());

The important point here is that, method Average() is not a method of the List. It’s available due to LINQ. Such methods are called extension methods. That is basically used to extend the functionality of List in this example. Saving the manipulated collection to an array, dictionary or a list is also possible by the extension methods such as ToArray(), ToDictionary(), ToList().

For a programmer using .net 2.0 for and the previous versions for some time LINQ might seem a bit odd and alien. But when you look deep in to it, it does cuts off lot of code and thinking which you would invest to do some complex work out. For example a simple selection from a list, will require you to traverse through a loop and then check for a condition and extract the data.Linq makes this task easy by giving a whole new look in to the Visual Studio 2008 environment, giving the new key words and a lot of functionality to what you want to achieve.


Post a Comment

<< Home