Tuesday, August 19, 2008

What is LINQ?

LINQ stands for Language-Integrated Query is a set of features in Visual Studio 2008 (code name "Orcas") that extends powerful query capabilities to the language syntax of C# and Visual Basic. Microsoft previous efforts (Windows Communication Foundation WCF, Windows Workflow Foundation WWF, Windows Presentation Foundation WPF, Windows CardSpace and LINQ) are integrated in this Studio. LINQ introduces standard, easily-learned patterns for querying and updating data, and the technology can be extended to support potentially any kind of data store. Visual Studio 2008 includes LINQ provider assemblies that enable the use of LINQ with .NET Framework collections, SQL Server databases, ADO.NET Datasets, and XML documents.

From past one and half years Anders Hejlsberg team has done a wonderful job in overcoming the gap between the data impedance. Mr. Anders team gives a native syntax to developers in the form LINQ to C# and VB.Net for accessing data from any repository. The repository could be in memory object, database (still MS SQL Server only) and XML files.

What is LINQ?
Basically LINQ address the current database development model in the context of Object Oriented Programming Model. If some one wants to develop database application on .Net platform the very simple approach he uses ADO.Net. ADO.Net is serving as middle ware in application and provides complete object oriented wrapper around the database SQL. Developing application in C# and VB.Net so developer must have good knowledge of object oriented concept as well as SQL, so it means developer must be familiar with both technologies to develop an application. If here I can say SQL statements are become part of the C# and VB.Net code so it’s not mistaken in form of LINQ. According to Anders Hejlsberg the chief architect of C#.

Some of the examples are:

Select:
This sample code prints a sequence of integers one greater than those in an input array. The sample uses the expression in the select clause to add one to each element in the new sequence.


public void Linq6()
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var numsPlusOne = from n in numbers select n + 1;

Console.WriteLine("Numbers + 1:");
foreach (var i in numsPlusOne)
{
Console.WriteLine(i);
}
}

Result:

Numbers + 1:
6
5
2
4
10
9
7
8
3
1

Where
This sample prints each element of an input integer array whose value is less than 5. The sample uses a query expression to create a new sequence of integers and then iterates over each element in the sequence, printing its value.
 
public void Linq1()
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

var lowNums =
from n in numbers
where n < 5
select n;

Console.WriteLine("Numbers < 5:");
foreach (var x in lowNums)
{
Console.WriteLine(x);
}
}

Result:

Numbers < 5:
4
1
3
2
0

OrderBy

This sample prints an alphabetically sorted version of an input string array. The sample uses orderby to perform the sort.

public void Linq28()
{
string[] words = { "cherry", "apple", "blueberry" };

var sortedWords =
from w in words
orderby w
select w;

Console.WriteLine("The sorted list of words:");
foreach (var w in sortedWords)
{
Console.WriteLine(w);
}
}

Result:
The sorted list of words:
apple
blueberry
cherry

There are 101 sample snippets available on MSDN.

2 comments:

Anonymous said...

Very Good Writeup about LINQ... Sample programs are simple but explains us more about LINQ...

You may be interested in this video which explains more about LINQ

http://wtv.watchtechvideos.com/topic180.html

KIRAN said...

Thank you for the excellent Webcast link, it is very useful indeed.