Learning to Like Linq or, Loving the Linq Loquacious

Once upon a time, long, long ago, I was working at a company that gave their engineers some shiny, state-of-the-art, new-fangled HP calculators. They were awesome; instead of battery-draining LEDs for display, they used something called an LCD. And, they had buttons galore: more buttons than any other calculator at that time. We positively drooled at their appearance. They looked something like this:


But when it came time to do something useful, like adding two numbers, instead of entering:

    3 + 5

You had to enter:

    3 5 +

What the heck? Crazy! It's all mixed up! It was using something called: Reverse Polish Notation (postfix instead of infix notation).

A small number of people used their calculators for a few weeks but eventually everyone retired them to a bottom desk drawer. After spending our entire lives with infix notation, no one wanted to change: Learning something new is hard; unlearning something old is harder.

Years later, while I was becoming a software engineer by going to school at night, we studied designing calculator software. Then it made sense: Pushing and popping operators and operands on and off a stack is very efficient for the developer and the computer. Sadly, it is unfriendly for the user.

I believe Linq suffers from the same malady.

Here is a pseudo SQL statement:

    select MyNumber from MyTable where MyNumber > 1 

 Here is a similar Linq query:

    from MyNumber in MyList where MyNumber > 1 select MyNumber

What the heck? Crazy! It's all mixed up!

It's Reverse Polish SQL!

How many developers have looked at Linq the first time and said, "No thanks, please go away"? A plethora, I'm sure.

Recently however, I took on a contract that required Linq knowledge. Guess what? You can use the Linq library functions, which are extension methods, without having to delve into the abomination of Reverse Polish SQL.

Here's an example, create a new array that contains the elements common to two other arrays:

    int[] MyList1 = new int[] { 1, 2, 3, 4 };
    int[] MyList2 = new int[] { 1, 7, 3, 12 };
    int[] MyList3 = MyList1.Intersect(MyList2).ToArray();

One line of code! The Intersect method is a Linq extension method. Being able to accomplish a complex task with one line of code is exhilarating and empowering. Did I really say exhilarating? I've got to get out more.

Here are a few more Linq extension method examples:

    double Average = MyList1.Average();
    int Sum = MyList1.Sum();
    MyList3 = MyList1.Where(x => x > 2).ToArray();

 OK, Linq is awesome. I am sold. To get a list of available functions, right-click on a Linq Extension method and select "Go To Definition". There are too many functions and overloads to list here. The functions above are in the System.Linq namespace in the static Enumerable class. There are other classes in the namespace but Enumerable is the easiest one to jump into. Here are the others: Linq Namespace.

I hope someone finds this useful.

Steve Wellens

BTW, I have gotten used to the abomination of Reverse Polish SQL. Hmmm, I wonder where that old HP calculator went to.



  • I am the nerd at work because I still program with my rpn at hand. W00t!

  • I don't really think you can compare LINQ to reverse polish notation; but sure...they did change it from SQL so I get your point about 'unlearning'; and I remember much debate around 2005 about what they should do - specifically I remember the VB.NET team was first going to use a more SQL syntax but then changed to be the same as C#.

    I personally always found the LINQ syntax really easy and logical. But I will admin I attended the PDC in 2005 and sat on the toilet at my hotel room in LA reading Anders 20 page whitepaper which just made it all seam so simple.

    I always found it really only took a few hours to get your head around the basics. But I personally have done much more stuff with LINQ against in-memory data structures rather than SQL.

    Regarding beginners: I find if you just write some LINQ, inexperienced programmers who have never used LINQ will pick up your code quite quickly and understand it.

    For me, LINQ and probably generics are some of the real highlights in my 30 years of programming (I am only 38, but started programming in primary school ;-)


  • "But I will admin I attended the PDC in 2005 and sat on the toilet at my hotel room in LA reading..."


  • I have created some projects in my University days with (prefix,infix and post fix) Polish Notations.

    Thanks for sharing :)

  • I totally agree. Linq is more than just syntax. The query syntax added to C# (and VB.Net) kinda sucks, but the extension methods provided in the System.Linq namespace are the type of thing that once you start using, it becomes hard to imagine life without them.

  • RPN easy to read is not;
    RPN easy to code is.

    Why do I think of Yoda every time I see RPN?

Comments have been disabled for this content.