« Groovy : A case for Java's Scripting Language. | Main | SCA - Service Component Architecture, your SOA's working pieces. »

November 20, 2005

LINQ : Language Integrated Query.

Searching data, querying information, localizing records -- or whatever other verb you wish to use -- is possibly one of the most common scenarios in software development. Typically, this process takes place accessing a relational database through the pretty much defacto language SQL ("Structured Query Language"), but having access to the same intuitive syntax in other software tiers is about to change for the better with LINQ Project .NET Language Integrated Query from Microsoft.

[Entry continues to the left and below ad ]

LINQ will form part of C# 3.0 and future versions of the .NET platform, along with other .NET language in the longer term. But what does it actually mean to have SQL type capabilities in a language like C# ? If you think about the data manipulation process in other areas, besides the relational database scenario, the process is extremely common.

How many times haven't you had a group of objects in an array or collection and had to get "All of the elements that started with the letter A" or "Grouped the elements that satisfied two conditionals" ? Even if you are a newbie to development, I would guess its been very often. The process in mainstream languages like Java and the current C# version (2.0) implies using loops and or regular expressions to get the desired results.

But on occasions this procedure can be rather complex to code, and this is exactly the process LINQ tries to tackle, shifting the use of a well-known syntax like SQL and having the capacity to apply it not only to relational databases but to in-memory data. Here is a short example:

The source:
using System;
using System.Query;
using System.Collections.Generic;

class app {
  static void Main() {

    string[] names = { "Burke", "Connor", "Frank", 
                       "Everett", "Albert", "George", 
                       "Harris", "David" };

    IEnumerable<string> expr = from s in names 
                               where s.Length == 5
                               orderby s
                               select s.ToUpper();

    foreach (string item in expr)
      Console.WriteLine(item);
  }
}
The results for this program would be :
BURKE
DAVID
FRANK
Sample source Getting Started with Standard Query Operators

This example shows the use of popular SQL keywords like select, from, where and order by for manipulating an array of data and reducing the amount of code -- hence time -- needed to extract a particular group of results.

I recently saw a few posts on the web, saying LINQ is in violation of the classical tiered architecture principle -- not mixing data, business logic and GUI functions in the same design -- since the approach is rooted in the language used in databases, but I digress.

LINQ is not about violating tier separation and allowing you to access data wherever you want, just like using a regular expression or loop for getting the data you want -- in a graphical or business logic tier -- is not a violation of this same principle. LINQ is about filtering and/or sorting data using programming structures that a more intuitive and familiar to developers.

The design imposed by LINQ will be an interesting development to follow, especially how other language designers react and possibly adopt a similar syntax for manipulating in-memory data and other types of information in this fashion.

[Comments below ad ]

Posted by Daniel at November 20, 2005 6:53 PM


Comments


Post a comment




Remember Me?

(you may use HTML tags for style)

Track back Pings

Track Back URL for this entry:
http://www.webforefront.com/mtblog/mt-tb.cgi/30.