Falling in Love with LINQ - Part 1 (Introduction)

by Larry Spencer Tuesday, November 1, 2011 9:09 PM

This is the first of a series of posts to follow up my presentation at New England Code Camp 16. To get into the spirit of things, you can watch this video about project management.

Lord of the Rings, Extended Edition: Burn It

And then watch this video about how the orcs develop software.

The Creation of the First Uruk Hai (Sorry about the brief ad at the beginning.)

After you watch the videos, you'll be able to appreciate what it means to write code like an orc. It's slash-and-burn, leaving ugliness everywhere.

Suppose an orc were to get an assignment to produce a list of the 15 largest cities in America. He might produce code like the following (extracted from Demo1 in the complete solution here.)


static IEnumerable<City> GetLargest_Orc(int numDesired)
    var workList = new List<City>();

    var returnList = new List<City>();
    for (int ix = 0; ix < numDesired; ++ix)

    return returnList;

static int CompareCitiesByPopulationDescending(City c1, City c2)
    return c2.Population - c1.Population;


The orc begins by copying the original list from the Cities member (not shown) to a new list, so the original will not be disturbed by the sorting operation. Then, he plods through the workList one at a time, adding the desired number to the result, which is returned.

So what's wrong with that? Nothing. It does work. However, as we'll see in upcoming posts it's a maintenance nightmare. In addition, it's not nearly as clear as what we'll see next.

Here's how the same code would be developed by the good guys in Lord of the Rings -- the elves. This is our first foray into LINQ. Some say that LINQ stands for Language Integrated Query. I say it stands for Lovely and Intelligent, .NET's Queen.


static IEnumerable<City> GetLargest_LinqQuery(int numDesired)
{ // The empty space shows how much less code this requires.

    var query = from c in Cities
                orderby c.Population descending
                select c;

    return query.Take(numDesired);



You can see that this is much more readable. It's the query form of link. I prefer the next form, known as the method form. I'll tell you why in a moment.


static IEnumerable<City> GetLargest_LinqMethod(int numDesired)

    return Cities.OrderByDescending(c => c.Population)



Why do I like the second form better? Several reasons:

  • Its Lambdas resemble Expressions (which we’ll see later), so we want to get used to it
  • It gives much better intellisense at every turn: typing, hovering and F12 (Go to Definition).
  • It is more fully implemented. Note that the Take method had to be coded separately in the fist firm, but flows naturally in this one.
  • Real programmers use classes and methods, not keywords. ;)

In Part 2, I'll explain how LINQ works.


All | General | Talks

About the Author

Larry Spencer

Larry Spencer develops software with the Microsoft .NET Framework for ScerIS, a document-management company in Sudbury, MA.