Welcome to MSDN Blogs Sign in | Join | Help

The LINQ Farm: LINQ for Beginners

This is the first in a series of posts on C# and LINQ. These posts will describe a natural, easy to understand technique for querying data. When using LINQ, simply declare the question you want to ask, and then sit back and wait while the computer analyzes your query and finds an optimal way to retrieve your answer.

The development of LINQ involved input from many of the best minds at Microsoft. Anders Hejlsberg, researchers at the Microsoft research centers in Redmond and in Cambridge, England, the C# developer and test teams, the C# program managers, and others all contributed to the development of this language. As a result, it represents one of the major technical achievments that has come out of the Microsoft Developer Division in recent years.

When you hear talk about querying data, it is easy to imagine the kind of data that resides in a database. And indeed, you can use LINQ to query data in a database. However, LINQ does more than just run SQL queries. It can be used with almost any kind of data.

The acronym LINQ stands for Language Integrated Query. This means that LINQ is a technique for querying data that has been integrated into the C# language. For database programmers, this means that you don't have to embed SQL strings in your code anymore. Instead, a query language will be built into C#. For XML programmers, this means that you don't have to learn XPath, instead, a query language will be built into C#. You can, of course, continue to use any technology that you prefer, or any combination of technologies that seems optimal in a particular case. But LINQ is now built into the C# language and offers a native solution.

A wide variety of data sources will now be accessible through LINQ. You can use it to retrieve data from an array, or from a collection, or from any data source that supports IEnumerable or IQueryable. You can even query your own program and ask how many of its methods are public, or which classes reside in a particular namespace.

LINQ can, at least in theory, be part of any .NET language. For now, that means primarily C# and VB, but there has been talk of integrating LINQ into other languages, such as PHP and Python. LINQ will be released as part of the Orcas version of Visual Studio. At the time of this writing it is available in various Microsoft CTP's, and of course it will be part of the Orcas beta when it ships.

LINQ in Action

It's time to take a peak at the LINQ syntax as it appears in the May CTP. Note that LINQ is still under development and the syntax is still undergoing minor modifications. But for now, I strongly recommend installing the May CTP and taking LINQ out for a spin.

The code shown in Listing Two demonstrates a somewhat unorthodox way to use LINQ to query an array of string. I say it is unorthodox because I sidestep a few common LINQ idioms in order to write code that will look as familiar to you as possible. In future posts, I will talk more about the preferred way to write LINQ queries, but for now I want to limit the number of new ideas I introduce in this first pass over the subject.

Listing One: Simple test harness for running the code in Listing One.

   1:  using System;
   2:   
   3:  namespace LinksToLinq
   4:  {
   5:      class Program
   6:      {
   7:          static void Main(string[] args)
   8:          {
   9:              WordQuery.Run();  
  10:              Console.ReadLine();
  11:          }
  12:      }
  13:  }

Listing Two: Querying an array of string.

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Query;
   4:   
   5:  namespace LinksToLinq
   6:  {
   7:      class WordQuery
   8:      {
   9:          public static void Run()
  10:          {
  11:              string[] EinsteinQuote = new string[] {"space", 
  12:                  "detached", "from", "any", "physical", 
  13:                  "content", "does", "not", "exist" }; 
  14:   
  15:              IEnumerable<string> selectedWords =
  16:                  from p in EinsteinQuote
  17:                  where p.Equals("any") != true
  18:                  select p;
  19:   
  20:              foreach (string word in selectedWords)
  21:              {
  22:                  Console.WriteLine(word);
  23:              }         
  24:          }
  25:      }
  26:  }

You can compile and run the code in Listing Two by first creating a C# console application, and then placing a call to WordQuery.Run() in the Main statement of your program, as shown in Listing One. A run of the application should generate the following output:

space 
detached 
from 
physical 
content 
does 
not 
exist

As you can see, the code prints out all the words from our string array except for the word "any." The LINQ syntax is close enough to English that this output should not come as a surprise.

Look at the code in lines 15 through 18. You can fairly readily translate them into the following English sentence: "Select all the words from the string array that are not equal to the word 'any.'"

If you prefer, you can make the English translation match the code a little more precisely: "Select all the words from the string array where the words you choose are not equal to the word 'any.'" In this version, we see all the major elements of the syntax found on lines 15 through 18.

Simply Declare What You Want Done

If you have read much about LINQ, then you have probably heard that it is a declarative language. I'll explain exactly what that means in a future post. For now, however, all you need to know is that LINQ allows you to "declare" what you want done, and then sit back and let the computer figure out how to do it.

You don't have to write a series of imperative sequential statements that laboriously describe how to complete your task. All you have to do is say what you want to do, and then let the computer figure out how to best go about doing it. This is the advantage of declarative code: We simply declare what we want done, and then advanced logic built into the language discovers the best way of doing it.

Before LINQ, we would have accomplished the task shown in Listing 1 by iterating over all the words in the string array, and testing to see which ones were equal to the word "any." That's a very laborious, sequential way of accomplishing a task. With LINQ, we simply say: "Hey, how about you give me all the words from this array that aren't equal to the word 'any.'" You just declare what you want done, and then let the computer figure out how best to do it. In a simple case like this, we might not need much help, but when writing a complex SQL query, it is good to have a little help optimizing your code.

I've said that LINQ can be used in several different domains. It can be used to query arrays, databases, XML files, class hierarchies, and numerous other types of data. In each case, you are going to be able to use either the same declarative syntax you've seen so far, or a similar syntax crafted for a particular domain. You might, for instance, use LINQ to ask questions like these:

  • Select all the rows from this database where the country field is equal to 'USA.'
  • Find all the methods from this class that are declared to be public.
  • Return all the numbers in this array that are evenly divisible by 25.
  • Group together all the strings in this array that begin with the letter 'E', 'F' and 'G'.
  • Return all the elements from this XML file where the country attribute is equal to Lithuania.

The select and where keywords lie at the heart of the query expressions you create in LINQ. Third parties could redefine these keywords, and there are other common words such as join and groupby. The only real limitation here is that you must query things that support the IEnumerable or IQueryable interface. Exactly what that means to us as programmers is a subject to be explored in later posts. 

Summary

In this post I've outlined a few of the basics you need to know to get started with LINQ. As often happens when exploring a new topic, I've ended up raising nearly as many questions as I've answered. That's the way things should be though, and in future posts I'll dig into each of these questions, and explore LINQ in considerable depth.

In particular, I'll talk more about the new keywords select and where, and about that mysterious variable P that appears on lines 16, 18 and 19. Querying XML and querying your own code are also subjects ripe for discussion.

LINQ is the big new feature in C#, and those of us who want to be ready for the future need to begin exploring this subject in earnest. We are on the verge of a huge change in the way we think about writing C# programs, and we can look forward to many exciting new developments that will open up lots of new territory.

Read the next post in this series.

kick it on DotNetKicks.com
Published Sunday, October 29, 2006 12:57 AM by Charlie Calvert
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Community Convergence IX

Orcas and LINQ development has the full attention of nearly everyone on the C# team these days, and the

Monday, October 30, 2006 1:51 PM by Charlie Calvert's Community Blog

# The LINQ Farm: LINQ for Beginners

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Monday, October 30, 2006 4:15 PM by DotNetKicks.com

# re: The LINQ Farm: LINQ for Beginners

So is LINQ done?

Teaching CTP code seems a bit off to me.

They should start by updating their installation to remove the necessity to have VB.NET installed.

Tuesday, October 31, 2006 12:17 PM by Steve

# New namespace found!

You forgot to mention about the System.Query namespace, which should also be a subject of discussion as well :)

Friday, November 03, 2006 3:40 AM by Indra Bayu

# Charlie Calvert's Technical Blog Index

This is index to the various technical posts I have created for this blog. As I add to each section,

Sunday, November 12, 2006 1:39 AM by Charlie Calvert's Community Blog

# C# IDE Chat, November 28, 2006

These transcripts will eventually be published here , but while we're waiting for that to occur, I'll

Tuesday, November 28, 2006 8:29 PM by Charlie Calvert's Community Blog

# Charlie Calvert's LINQ posts

Charlie Calvert , who some of you may know for the years he spent at Borland or for his books on Delphi,

Tuesday, December 05, 2006 11:08 AM by Fabrice's weblog

# Charlie Calvert's LINQ posts

Charlie Calvert , who some of you may know for the years he spent at Borland or for his books on Delphi,

Tuesday, December 05, 2006 11:08 AM by Linq in Action News

# Charlie Calvert's LINQ posts

Charlie Calvert , who some of you may know for the years he spent at Borland or for his books on Delphi,

Tuesday, December 05, 2006 11:09 AM by Fabrice's weblog

# > Fabrice's weblog: Charlie Calvert's LINQ posts

Charlie Calvert , who some of you may know for the years he spent at Borland or for his books on Delphi,

Tuesday, December 05, 2006 1:48 PM by Prog

# re: The LINQ Farm: LINQ for Beginners

mmm.. nice design, I must say..

Saturday, February 17, 2007 11:59 AM by ...

# re: The LINQ Farm: LINQ for Beginners

Hi Could you provide an example of using .ANY() operator in LINQ where I want to compare a table field with the output (multiple rows) of a sub query. Actually I wanted to use it something similar to "IN" clause in SQL.

Tuesday, February 20, 2007 10:29 AM by Raj

# re: The LINQ Farm: LINQ for Beginners

Lavoro eccellente! ..ringraziamenti per le informazioni..realmente lo apprezzo: D

Saturday, February 24, 2007 8:51 AM by ...

# re: The LINQ Farm: LINQ for Beginners

The information I found here was rather helpful. Thank you for this.

Monday, February 26, 2007 3:06 AM by ...

# re: The LINQ Farm: LINQ for Beginners

luogo interessante, soddisfare interessante, buon!

Sunday, March 04, 2007 11:40 PM by ...

# re: The LINQ Farm: LINQ for Beginners

Luogo molto buon:) Buona fortuna!

Saturday, March 10, 2007 2:42 PM by ...

# re: The LINQ Farm: LINQ for Beginners

Stupore! ho una sensibilit molto buona circa il vostro luogo!!!!

Monday, March 19, 2007 1:34 AM by ...

# re: The LINQ Farm: LINQ for Beginners

Interessare, molto interessante. Come avete fatto questo?

Tuesday, April 10, 2007 5:44 AM by ...

# re: The LINQ Farm: LINQ for Beginners

L'information interessante que vous avez! I'am allant revenir bientot.

Friday, April 13, 2007 12:43 AM by ...

# re: The LINQ Farm: LINQ for Beginners

Du musst ein Fachmann sein - wirklich guter Aufstellungsort, den du hast!

Saturday, April 14, 2007 5:29 AM by ...

# re: The LINQ Farm: LINQ for Beginners

Luogo molto buon:) Buona fortuna!

Monday, April 16, 2007 7:25 PM by ...

# re: The LINQ Farm: LINQ for Beginners

Charlie, you are my favorite author who presents technical ideas in a precise and understandable way. Keep up the good work!

Friday, May 11, 2007 9:29 PM by Jonathan Ulfeng

# LINQ Farm: Querying a Collections of Integers

This simple example shows how to use LINQ to Objects to query a collection of Integers. Some readers

Monday, November 05, 2007 1:02 AM by Charlie Calvert's Community Blog

# LINQ Farm: Querying a Collection of Integers

This simple example shows how to use LINQ to Objects to query a collection of Integers. Some readers

Monday, November 05, 2007 1:24 AM by Noticias externas

# Good site

<a href= http://index1.gnusmu.com >dob</a>

Saturday, January 19, 2008 9:55 AM by Vilyamzq

# Good site

<a href= http://index1.tolant.com >best wsternhotels</a>

Wednesday, January 23, 2008 5:00 PM by Vilyamwq

# Good site

<a href= http://index1.tolant.com >best wsternhotels</a>

Wednesday, January 23, 2008 5:00 PM by Vilyamwq

# Good site

<a href= http://index1.tolant.com >best wsternhotels</a>

Wednesday, January 23, 2008 5:00 PM by Vilyamwq

# Lipitor side effects.

Lipitor anxiety. Side effects from lipitor. Lipitor side effects intestine. Generic version of lipitor. Lipitor. Lipitor grapefruit juice. Tremors from lipitor.

Sunday, July 20, 2008 10:53 PM by Lipitor fibromyalgia.

# Good site

<a href= http://aseeds.one.angelfire.com >transvestite rockstar</a>

Friday, November 28, 2008 1:29 AM by Olgunka-nu

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker