Wednesday, February 20, 2008

LINQ

LINQ, Language Integrated Query is a new feature in .Net 3.5. Tradiationally, a programmer
needs to different query language to query different datasources, for example, data sources like
Sql databases, XML and in-memory objects. This meant the programmer needed to learn
different query languages for each and every technology. There was another problem as well, queries were
written in string form and cannot be validated at complie time.
LINQ tries to address these problems, by giving the programmer a singel query language
for any data source for which providers are available.
The cool thing about LINQ is, it is in-built into the language such as C# and VB.Net and Visual studio provides Intellsense support.
This is a sample query of what LINQ can do:

public void Linq11() {
List products = GetProductList();//Get an ArrayList of products, not from the database

var productInfos =
from p in products
select new {p.ProductName, p.Category, Price = p.UnitPrice};

Console.WriteLine("Product Info:");
foreach (var productInfo in productInfos)
{
Console.WriteLine("{0} is in the category {1} and costs {2} per unit.", productInfo.ProductName, productInfo.Category, productInfo.Price);
}
}

No comments:

Post a Comment