break

LINQ: a quick start

If you want a basic quick start in LINQ this is going to help you.

LINQ according to the research I did in MSDN is a “set of extensions to the .NET Framework that encompass language-integrated query, set, and transform operations.” In MSDN they define LINQ in simple terms, and state that “LINQ is a series of language extensions that supports data querying in a type-safe way”. The last interesting fact I found in MSDN about LINQ is that “the data to be queried can take the form of XML, databases, objects, and so on”.

There’s a lot of theory about LINQ in MSDN and in the internet, but my purpose is to give you a quick start in how to start coding LINQ applications.

The first step is to download the “Language Interated Query, May 2006 Community Technology Preview”. The link to this preview is here.

The importance of installing this extension is that if you go to Visual Studio 05, and press File->New->Project , you will see in your Visual C# tree a node that says “LINQ Preview” which will give you the option to create a LINQ Console Application, LINQ Windows Application, LINQ Library, and LINQ WinFX Application.

For this introduction I’m going to choose a LINQ Windows Application. When a LINQ Windows Application is created you will be able to see DLL’s needed for LINQ applications such as: System.Data.DLinq, System.Data.Extensions, System.Query, System.Xml.XLinq.

Now that you have your LINQ Windows Application created, the task will be the following:
Display an array of strings ordered alphabetically in a listbox.

This is how you can do this:

private void Form1_Load(object sender, EventArgs e)
{
string[] names = { "Daniel", "Lesseps", "Isaacs", "Diaz" };
var namesToDisplay = from name in names
orderby name
select name;
foreach(var name in namesToDisplay)
listBox1.Items.Add(name);
}

When our form loads the first line declares an array of strings that contains 4 words.

The next line is a sample LINQ query, and I will explain this line as I understand it (which means that if you have any input on my explanation it will be appreciated) . As you can see LINQ queries start with “var” which indicates it’s a local variable. The LINQ query contains clauses like: FROM, WHERE, SELECT,ORDERBY, GROUP BY, and others. This example starts with “from name in names”. The part “in names” is very important since it’s specifying the list that is going to be queried. After this I’m ordering the names alphabetically using “orderby name”. The last line of the query is the “select name(you can think of name as the name of my column)”. SQL queries start with the SELECT clause; however, LINQ queries end with the SELECT clause.

var namesToDisplay = from name in names
orderby name
select name;

After this we iterate the results of the query using a foreach, and each statement returns a name.

As you can see, SQL queries can now be integrated in C#.
Very interesting! This is a basic example,I will write more complex examples later on.

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.