.Net Hive

A blog about Microsoft .Net Platform & other industry happenings

Language Integrated Query – LINQ

In my previous post, I introduced some of the extensions provided in ADO.NET 3.5 and I told you that we will be talking about each of the extension individually later on.

To start, today I have picked LINQ. Language Integrated Query aka LINQ, is all about achieving query operations in a well comprehensive and open form. It allows you to query data regardless of any type, May it be relational databases, XML or Objects. Considering this phenomenon of querying across any kind of data source, this will be a great breakthrough and major shift in how our programmers deal with data related tasks. Having the possibility at your fingertip to query any kind of data will really change the way we program.

LINQ provides a set of patterns for querying any kind of data, whether it has a particular object model implemented or not. LINQ specifies how an object can be a “queryable,” with all the basic query operations needed, like: SELECT, WHERE, GROUP BY, ORDER BY, JOIN along with many other new operations like SelectMany, GroupJoin, TakeWhile, SkipWhile, ElementAt etc.

The syntax is the part where the real ease and flexibility shows up. You can have any language, that is supported on .Net Platform;  to be able to support LINQ.  C# and VB.NET has this built in features to interfacing LINQ. An example, where you have an array of Employees objects and you want a name of an employees having salary greater then 10000, you can write this query that looks like this:

var query_results =  from c in Employees
                                           let sal = 10000
                                           where c.Salary < sal
                                           select c.Name;

foreach (var result in query_results)
{
          Response.Write(result.ToString());
}

The above example mimics a scenario for a relational database. What happens if we have a dynamic data source like an RSS feed? Here is the simple example for that

XElement orders = …;
int order_amount = Convert.ToInt32(orders.Element(“amount”));

As you have seen that, LINQ is a way to go for data related operations due its flexibility and its data source independence. I have tried to give you an overview of LINQ but there are whole lot of information and resources available for you to get your hands dirty in LINQ :). I’m including some of the references below for your help:

Happy Reading

Waqas Ahmed

Filed under: .Net, , , ,

Pages

Categories

Archives

.Net Hive @ Twitter

Blog Directory by Blog Flux
Computers/Tech blogs