LINQ : An Introduction

In this article, we will be discussing a very useful Microsoft technology called LINQ..which was introduced with .Net 3.0.

So, Lets see what LINQ is..

LINQ stands for Language Integrated Query. It is Microsoft’s new technology for powerful and easy general purpose data access in a type-safe way.

Now the question arises : What was the need to develop LINQ ??

Well.. the problem was that.. Traditionally, our data source used to determine our selection of a low-level data access API. For example, to fetch relational data from a database table, we would use ADO.NET, to parse XML data, we might use some combination of XPath and XML DOM APIs. Each data access API requires its own specialized syntax (SQL queries, as an example), and typically forces us..the developers to shuffle data between their business objects and the data access API.

All of the diverse data access strategies and APIs means there is a tremendous amount of mental energy expended to build the basic data features needed in every business application. LINQ helps us to eliminate this mental overhead.

Basically, It defines a set of standard query operators in the System.Linq namespace to select, filter, aggregate, and partition data from any type that implements the IEnumerable or IEnumerable <T> interfaces. The standard query operators are compliant with the Common Language Specification (CLS) and work inside any .NET language that supports generics. LINQ gives .Net developers the ability to query and transform data using any .Net Language of their choice.

LINQ is very similar to SQL. So, if you are used to using SQL to query databases, you are going to have something of a head start with LINQ.

This means that whether the data lives in XML, or database, or some collection of objects, we can run through the desired data in a common way..by simply writing SQL like queries.. Query expressions, like “from”, “where”, and “select”, “order by” and all the others you know and love from SQL can now be used in C# and VB .Net.

Lets now jump on to our first code and see how does LINQ code looks like..

In the following C# code, we are using LINQ to query a collection of Strings.

string[] flowers = { "Rose", "Lotus", "Jasmine", "Lily" };
IEnumerable query = from n in flowers
           where n.Length == 4
           orderby n descending
           select n;
foreach (string flower in query)
{
   Console.WriteLine(flower);
}

Inside this code is a LINQ query expression. The code will print out all the flower names in descending alphabetical order. We will discuss on the LINQ syntax in our future article.

The next query produces the same results, but queries data inside an XML fragment.

XElement flowers = XElement.Parse(
        @"<flowers>
           <flowers>Rose</flowers>
           <flowers>Lotus</flowers>
           <flowers>Jasmine</flowers>
          <flowers>Lily</flowers>
         </flowers>"
        );


IEnumerable query = from n in flowers.Elements("flowers")
          where n.Value.Length == 4
          orderby n.Value descending
          select n.Value;
foreach (string flower in query)
{
   Console.WriteLine(flower);
}

Although we made some slight changes to dig into the XML, the operators inside the query expression remains the same. LINQ query expressions are similar to SQL SELECT commands with FROM, WHERE, ORDERBY, GROUP, AND SELECT clauses.

Microsoft also designed LINQ to integrate closely with .NET languages and their environment. When typing the above queries, Visual Studio provides Intellisense for auto-completion. The compiler can perform syntax and type checking on query expressions.

Thus, we discussed the use of LINQ and what LINQ is.. We also saw an example of implementation of LINQ. We will be carrying on with the LINQ in our coming articles..

References :

http://msdn.microsoft.com/
• http://www.odetocode.com/
• http://www.programmersheaven.com/
• http://blogs.msdn.com/cyrusn/



No comments: