Click here to Skip to main content
15,861,168 members
Articles / Programming Languages / C#

Demystify LINQ in 10 Minutes

Rate me:
Please Sign up or sign in to vote.
4.62/5 (36 votes)
23 Jun 2014CPOL4 min read 94.1K   577   115   21
Journey of C# up to LINQ in less than 10/15 minutes

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:

C#
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:

C#
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:

C#
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

C#
delegate bool IsParticularState(City c);

Step 2

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

Step 3

C#
PrintCityInfo(cities, new IsParticularState(IsCalifornia));

Step 4

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

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:

C#
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:

C#
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:

C#
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:

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

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

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:

C#
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...

C#
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?

Update :  While explaning LINQ to folks coming in from traditional procedural programming background, I used this example to explain Lambda basics. Hope this will be useful to others - please let me know - LINQPad is used to test this code

C#
delegate bool diseven(int i);

void Main(){
    int[] mi = new int[]{2,3,4,5,1};
    "Count Even Numbers".Dump();
    diseven di =isEven;
    int counter=0;
    foreach(int i in mi)
     if(di(i))
       counter++;
    counter.Dump("using Traditional Loop");
    mi.Count(delegate(int i){
    return i%2==0;
    }).Dump("Using delegate");
    Func<int,bool> fm = i =>i%2==0;
    mi.Count(i=>fm(i)).Dump("using built in delegate Func");
    mi.Count(i=>i%2==0).Dump("using Lambda");
}

bool isEven(int i)
{
return i%2==0;
}

History

  • 26th June, 2008: Initial post
  • 22 June,2014 : Included Lambda example

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Wells Fargo
United States United States
MS Computer Science + Information Science
Architect at Wells Fargo [Wells Fargo Dealer Services -WFDS]
Blog: Profile

abhijit gadkari
abhijit dot gadkari at gmail dot com
Irvine-92618,CA

Comments and Discussions

 
Questionyour sample source zip file is empty Pin
fredatcodeproject2-Jun-15 22:29
professionalfredatcodeproject2-Jun-15 22:29 
GeneralThanks Pin
zircon74724-Jun-14 9:39
zircon74724-Jun-14 9:39 
QuestionThanks!! Pin
abhigad22-Jun-14 14:24
abhigad22-Jun-14 14:24 
AnswerRe: Thanks!! Pin
Member 1068560823-Jun-14 14:36
Member 1068560823-Jun-14 14:36 
QuestionThank you. Pin
Member 1013242329-Jun-13 3:33
Member 1013242329-Jun-13 3:33 
QuestionHate to Tell You This Pin
Patrick Harris3-Jul-12 17:39
Patrick Harris3-Jul-12 17:39 
GeneralMy vote of 5 Pin
Patrick Harris3-Jul-12 13:26
Patrick Harris3-Jul-12 13:26 
GeneralThanks... :) Pin
ZoDiAc8-Sep-09 20:30
ZoDiAc8-Sep-09 20:30 
GeneralThank You Pin
Lee Humphries21-Jun-09 18:47
professionalLee Humphries21-Jun-09 18:47 
GeneralWell written article Pin
dionliddell1-Jul-08 16:22
dionliddell1-Jul-08 16:22 
GeneralDoes not compile. Pin
elbertlev1-Jul-08 8:09
elbertlev1-Jul-08 8:09 
GeneralRe: Sample code will compile Pin
abhigad1-Jul-08 9:14
abhigad1-Jul-08 9:14 
GeneralCheck the supported C# versions Pin
Artiom Chilaru30-Jun-08 20:30
Artiom Chilaru30-Jun-08 20:30 
GeneralBoolean expressions Pin
Fregate30-Jun-08 19:56
Fregate30-Jun-08 19:56 
AnswerRe: Boolean expressions Pin
u7pro27-Nov-08 13:34
u7pro27-Nov-08 13:34 
GeneralCompliments, but fix spelling. Pin
jpmik27-Jun-08 0:50
jpmik27-Jun-08 0:50 
GeneralGood Pin
merlin98126-Jun-08 4:05
professionalmerlin98126-Jun-08 4:05 
QuestionWhat about Expression Trees Pin
Rafael Nicoletti26-Jun-08 2:00
Rafael Nicoletti26-Jun-08 2:00 
AnswerRe: What about Expression Trees Pin
abhigad26-Jun-08 7:18
abhigad26-Jun-08 7:18 
GeneralRe: What about Expression Trees Pin
abhigad5-Jul-08 17:07
abhigad5-Jul-08 17:07 
Generalnice ! Pin
Jeremy Alles25-Jun-08 22:53
Jeremy Alles25-Jun-08 22:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.