Language INtegrated Query - After all the discussions and sessions from Anders Heijlsberg, All I could say is, “This feature adds lot’s of Common Sense to the Language…And Not a Cryptic Rocket Science kinda feature
But, Just lots of essential & practical Common Sense…”
In Layman/Fresher terms, this can be explained with the following question “Whenever we build a application, we always interact with lots of data, and when it is relational data, we have SQL to manipulate the query with in the database systems. But, what happens to the data after retrieving from the Database Server ? Isn’t all the querying capabilities you had just few moments back is lost and you end up in using for…next loops and endless of other mechanism to manipulate the same data ? Next, How about in-memory objects ? traditionally they are always treated as collections and never had another fresh look of handling things… Finally What about XML Data, and ploethra of stuff like XQuery, XPath et al ?”
When most of our application always involves in manipulation of data…data…data, shouldn’t our language know about this and provide support to manipulate these data in a uniform manner and a flexible, simple ways ? and most importantly today we see object orientation has become a very common phenomena of software development, shouldn’t we need a technology to reduce the complexity for the developers for accessing and integrating data that is not natively defined in Object Oriented Technology ?
Whoa… You got it… The Answer is LINQ, A Brain child of Anders Heijlsberg, and he has architected this LINQ Project, with his own minimalistic approach, so we can definitely expect Evolution of LINQ in future.
The Way this is implemented is quite simple -
1. .NET API’s are now Query Enabled
2. .NET Languages Integrated query defines a general purpose standard query operators that allow,
a. Traversal
b. Filter
c. Projection
operations to be expressed in a direct yet declarative way
Now, It is up to the .NET Programming language to implement this feature, VB.NET Adopts known “Select …” ANSI SQL Query Approach, and C# uses a similar approach, and VC++ guys as usual need to access the API’s directly for their querying capabilities.
LINQ Also allows third parties to define their own set of domain specific standard query operators targeting their technology and also has the flexibility to replace the standard queries.
In order to Query XML, XLINQ uses an efficient and easy to use in-memory XML Facility to provide querying capabilities and NO it does not replace XPath/XQuery. XLINQ Just Extends them.
In order to Query Relational Data, DLINQ is build on integration of SQL based schema definitions into the CLR Type System, resulting in retaining strong typing and expressive power of the relational model, and NO DLINQ does not replace ADO.NET, it just extends or adds more value to ADO.NET
Here is a very very simple program given by Anders and Don Box to see LINQ in action and uses standard query operators to process contents of in-memory objects like Array
using System;
using System.Query;
using System.Collections.Generic;
If you were to compile and run this program, you’d see this as output:
class app {
static void Main() {
string[] names = { “Burke”, “Connor”, “Frank”,
“Everett”, “Albert”, “George”,
“Harris”, “David” };
IEnumerable expr = from s in names
where s.Length == 5
orderby s
select s.ToUpper();
foreach (string item in expr)
Console.WriteLine(item);
}
}
BURKE
DAVID
FRANK
Simple Ain’t it… Though it might not sound like a great at first look, think about the flexibility on complex stuff…
Now, Let’s Dissect the Code a bit,
IEnumerable expr = from s in names
where s.Length == 5
orderby s
select s.ToUpper();
couple of new stuff to notice
expr = a local variable, initialized with a Query Expression
The Query Expression uses 3 standard operators Where, OrderBy & Select
The Arguments passed to these operators are called as Lambda Expressions (much like delegates, but has a very insightful implementation, I shall blog a bit more on insights of Lambda Expression soon), these lambda expressions allow operators to define individual simple methods and finally they are connected using a dot notation. These are the simple blocks for LINQ, All of the above combine to serve as building blocks for extensible query language.
Actually, a feature every programmer requires, and all of a sudden it makes you think… “Whoa! How did I Program all these days without this LINQ ?!”
Anyways let me keep it short with this basic stuff, but do not underestimate the power of LINQ, you need to look deep inside the complexity reduced by LINQ.
You can try this at : http://download.microsoft.com/download/2/a/4/2a405b66-1b1c-4fca-bfbf-007aad63d307/LINQ%20VB%20Preview.msi
Okay, Stay tuned for some interesting stuff on insights on Lambda Expressions, Expression Trees, Deferred Query Evaluation et al…