Click here to Skip to main content
Email Password   helpLost your password?

Introduction

A lot of good articles are doing a great job of explaining LINQ. From syntax to concept and projects, LINQ is well covered. The aim of this article is not to repeat / recycle this material into one big blob. Check out the list of good books for details. My favorite is this little pocket reference.

The concept of this article is based on this short video. This article is a graph of how we reached up to LINQ, and the evolution of C# from 1.0 to 3.5. Is LINQ a new thing or just sweet syntax for old ugly code? In this article, we will look at the journey of C# up to LINQ in less than 10/15 minutes. So let's get started and demystify the LINQ.

We will need some scaffolding code. We will use a City class as follows:

class City
    {
        private string _name;
        private string _state;

        public City(string name, string state)
        {
            this._name = name;
            this._state = state;
        }

        public string Name
        {
            get
            {
                return _name;
            }
        }

        public string State
        {
            get
            {
                return _state;
            }
        }
    }

Let’s create a collection of cities and then iterate over this collection using foreach as shown below:

List<City> cities = new List<City>();
City c = new City("Santa Ana", "CA");
City c1 = new City("Irvine", "CA");
City c2 = new City("Bloomington", "IN");

cities.Add(c);
cities.Add(c1);
cities.Add(c2);

foreach (City tempCity in cities)
{
    Console.WriteLine("City Name : {0} and State : {1}",tempCity.Name, tempCity.State);
}

Let’s filter this collection based on some criteria. Say we want cities from California only. A very simple solution will be to add an if condition inside the foreach loop as shown below:

 if (tempCity.State == "CA")
   Console.WriteLine("City Name : {0} and State : {1}", tempCity.Name, tempCity.State);

This is a good enough solution. The problem with this code is “tight coupling”. C# 1.0 provided a delegate based composable solution to this problem. The following is the refactored version of this code:

Step 1

delegate bool IsParticularState(City c);

Step 2

static bool IsCalifornia(City c)
     {
         return c.State == "CA" ? true : false;
     }

Step 3

PrintCityInfo(cities, new IsParticularState(IsCalifornia));

Step 4

static void PrintCityInfo(List _cities, IsParticularState filter)
        {
            foreach (City localCity in _cities)
            {
                if (filter(localCity))
                {
                    Console.WriteLine("City Name : {0} and State : {1}", 
                                      localCity.Name, localCity.State);
                }
            }
        }

As shown in step 1, we added a delegate IsParticularState. Step 2 is the target method for this delegate. Step 3 is the refactored call to the foreach loop which takes a delegate and cities collection as an input parameter. Step 4 will output the city name and state.

Notice the filter instead of the simple if condition. filter(localCity) is a delegate call. By doing this, we decoupled the filter logic into a separate method:

static bool IsCalifornia(City c)

Still this is too much of a code for implementing a simple filter logic. Wouldn't it be nice if we don't have to add an additional filter method. Yes, that's where C# 2.0 anonymous methods will be helpful. So, the code above can be refactored as follows:

PrintCityInfo(cities,delegate(City ctemp){return (ctemp.State == "CA"?true:false);});

Notice the inline delegate and anonymous method. This is a C# 2.0 feature - anonymous because this method has no name.

This is a good move, but still needs a delegate code. What if we want to improve this code using C# 3.0. Instead of using anonymous methods, we can use lambda expression as follows:

PrintCityInfo(cities,ctemp=>ctemp.State=="CA");

Isn't this code elegant? From 7+ lines, a delegate and a target method call in C# 1.0 to less than half a line of code in C# 3.0. This is the power of lambda expression. Underneath, the compiler is doing all the heavy lifting for us. Lambda expression is a combination of implicit variable and anonymous methods. In our example, ctemp is an implicit variable and ctemp.State=="CA" is an anonymous method.

And now comes LINQ. What if we want to filter the end result on multiple conditions or want to sort the output based on some predefined order like city name? We can do this without LINQ. But LINQ provides an elegant solution for this as shown below:

PrintCityInfousingLINQ(from ctemp in cities 
                       where ctemp.State=="CA"
                       select ctemp);

static void PrintCityInfousingLINQ(IEnumerable _cities)
        {
            foreach (City localCity in _cities)
            {
                
               Console.WriteLine("City Name : {0} and State : {1}", 
                    localCity.Name, localCity.State);
               
            }
        }

Here we defined a new method PrintCityInfousingLINQ. This is the same as the PrintCityInfo method. Rather than taking a collection and delegate as an input parameter, PrintCityInfousingLINQ takes enumerator as an input parameter. Another important factor is generics. Generics add the type safety and provide all other well documented benefits.

Let's order the end result by city name. This is a typical order by clause in TSQL. With LINQ, this can be written as follows:

PrintCityInfousingLINQ(from ctemp in cities 
                       where ctemp.State=="CA"
                       orderby ctemp.Name 
                       select ctemp);

Without the orderby clause, Santa Ana will be the first result. With order by clause, Irvine will be the first record.

Conclusion

From the trivial if condition in C# 1.0 to LINQ in C# 3.0, there is a continuum of logic and syntax. Imagine writing...

from ctemp in cities 
where ctemp.State=="CA"
orderby ctemp.Name 
select ctemp;

... using if conditions or delegates. LINQ brings the much needed improvement on the syntax front in addition to its support for SQL, XML and objects. With LINQ, intent is closely matched with the language syntax.

At a midnight debate by the empty parking lot of a grad school, someone said something to the effect of –“power of any programming language is its ability to say very complex things very easily. Like The woods are lovely, dark, and deep, But I have promises to keep…

LINQ is one more step in the right direction. What do you think?

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralThanks... :)
ZoDiAc
21:30 8 Sep '09  
Easy to Learn Big Grin Smile
GeneralThank You
Lee Humphries
19:47 21 Jun '09  
Fantastic little article - Great work.

Exactly the sort of stuff I like to see on CP.

I just love Koalas - they go great with Bacon.

GeneralWell written article
D.R. Liddell
17:22 1 Jul '08  
Great how you go through the generations of C# to reach the final eloquent answer using LINQ. Thankyou!

Dion Liddell

GeneralDoes not compile.
elbertlev
9:09 1 Jul '08  
A small problem with tha example in the article. Should be:
List<City> cities = new List<City>();
GeneralRe: Sample code will compile
abhigad
10:14 1 Jul '08  
Sample code will compile. I double checked the source code and it is working. Mistake is in the code explanation and not in the sample project. It is fixed now.

Thanks.

Abhi
GeneralCheck the supported C# versions
Artiom Chilaru
21:30 30 Jun '08  
I like the fact that the article is written for C# 1.0-3.5 and if about LINQ Wink
GeneralBoolean expressions
Fregate
20:56 30 Jun '08  
Those expressions are equivalent, are n't they?

return c.State == "CA" ? true : false;

return c.State == "CA"

And IMHO the second verion is SHORTER!
AnswerRe: Boolean expressions
u7pro
14:34 27 Nov '08  
The first one is just stupid!
GeneralCompliments, but fix spelling.
Jean-Paul Mikkers
1:50 27 Jun '08  
This is just what codeproject needs, short and sweet. You have my 5.

To make it a perfect article, you should fix the spelling mistakes: you consequently write "lamda" instead of "lambda". Also:

- woudn't
- helpfule
- missing "the" or "it" in some places. Example: Underneath (the) compiler is doing all the heavy lifting for us.
GeneralGood
merlin981
5:05 26 Jun '08  
Nice done, and very easy to read. 5 from me


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
LINQ Exchange - Learn about LINQ and Lambda Expressions Rhabot - World of Warcraft Bot Make long URLs short with NeatURL.net ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

GeneralWhat about Expression Trees
tkrafael_net
3:00 26 Jun '08  
This article is about linq to objects. When you have Linq To Sql, this goes further beyound. Ive studied about expression trees and need to know more things about. Do you have some good articles explaining the usage and management of expression trees?
GeneralRe: What about Expression Trees
abhigad
8:18 26 Jun '08  
check out Charlie's blog post expression-tree-basics.aspx
He did a great job of collecting Links-to-LINQ at- Links-to-LINQ.aspx
10 min's version on expression tree is “work in progress”. For any specific question - shoot an email on abhijit dot gadkari at gmail dot com

Thanks.

abhi
GeneralRe: What about Expression Trees
abhigad
18:07 5 Jul '08  
I just found another resource on expression tree - a complete book with the title "C# Query Expressions and Supporting Features in C# 3." A free 239 pages preview of this book in PDF format is available at the following site
http://www.mindviewinc.com/Books/CSharp/Index.php Hopefully this book will cover the expression tree in detail.

abhi
Generalnice !
Jeremy Alles
23:53 25 Jun '08  
I liked the way you wrote your article, from basic "if" to delegate and then LINQ ! This is good to show the power of LINQ I think


Last Updated 26 Jun 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010