Tuesday, 24 July 2012

An Introduction to LINQ :

Introduction to LINQ


The official goal of LINQ family of technologies is to add "general purpose query facilities to the .NET Framework that apply to all sources of information, not just relational or XML data". One of the nice things about LINQ is that it integrates seamlessly with the existing .NET languages such as C#, VB.NET because the underlying LINQ API is just nothing but a set of .NET classes that operate like any other .NET class. In addition, the query functionality is not just restricted to SQL or XML data; you can apply LINQ to query any class as long as that class implements IEnumerable class.

As mentioned before, LINQ is a family of technologies that provide querying features and class patterns that DLinq and XLinq. DLinq (also referred to as LINQ to SQL) is specifically the version of LINQ that focuses on querying data from relational data sources. XLinq (LINQ to XML) is that aspect of LINQ that is geared towards querying XML data. To run the code samples supplied with this article, you need Visual Studio 2005 Professional RTM as well as LINQ May 2006 Community Technology Preview.

Simple LINQ Example

To start with, create a new Visual C# LINQ Windows Application project using Visual Studio 2005. Notice that the Visual Studio automatically adds the following LINQ namespaces.

using System.Query;
using System.Data.DLinq;
using System.Xml.XLinq;

Out of the above namespaces, System.Query namespace is the core namespace that exposes standard LINQ query operators.

As a start, open up the form add the logic that loops through an array of string using LINQ. To this end, add a command button to the form and modify its Click event to look like the following:

private void btnLoopThroughStrings_Click(object sender, EventArgs e)
{
string[] names = {"John", "Peter", "Joe", "Patrick", "Donald", "Eric"};
IEnumerable namesWithFiveCharacters =
from name in names
where name.Length < 5 select name; lstResults.Items.Clear(); foreach(var name in namesWithFiveCharacters) lstResults.Items.Add(name); } Note that the above code assumes that there is a list box named lstResults added to the form and this list box is meant to display the results of the query. If you were to compile and run this program, you would see the below output:


Let us walk through the code line by line.

First, you declare a string array called names.

string[] names = {"John", "Peter", "Joe", "Patrick", "Donald", "Eric"};

Then you loop through the names array using the LINQ syntax. As shown below, the new LINQ syntax code enables you to query from any type of data source in a way that is very similar to querying from a table in T-SQL.

IEnumerable namesWithFiveCharacters =
from name in names
where name.Length < 5
select name;

The above syntax is functionally similar to the T-SQL statement in that this allows you to query the names array using the from..where..select syntax. The main difference is the use of "from name in names" syntax in the beginning and the appearance of "select name" at the end.

Essentially, you retrieve data from an array of names using the "where" clause as a filtering mechanism. It is very important to note that it you need a way to refer to the objects inside the collection so that you can use standard query operators like where with those objects. In this case, "name" is used to refer to the each object inside the names array. Now that you have understood the basics of LINQ, let us extend our scope to query collections that contain objects.

No comments:

Post a Comment