Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Learning to Like LINQ or, Loving the LINQ Loquacious

, 30 May 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
You can use the LINQ library functions, which are extension methods, without having to delve into Reverse Polish SQL.

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 the 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.

License

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

Share

About the Author

Steve Wellens
EndWell Software, Inc.
United States United States
I am an independent contractor/consultant working in the Twin Cities area in Minnesota. I work in .Net, Asp.Net, C#, C++, XML, SQL, Windows Forms, HTML, CSS, etc., etc., etc.

Comments and Discussions

 
SuggestionImpressive but need something more PinmemberShashank Bisen1-Jul-11 19:20 
Generalinteresting PinmemberCIDev17-Jun-11 8:31 
GeneralMy vote of 3 PinmemberQwertie7-Jun-11 10:39 
GeneralReverse Polish SQL LINQ explination [modified] PinmemberJV99992-Jun-11 21:42 
GeneralRe: Reverse Polish SQL LINQ explination PinmemberSteve Wellens3-Jun-11 4:35 
GeneralRe: Reverse Polish SQL LINQ explination PinmemberJV99993-Jun-11 6:30 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 30 May 2011
Article Copyright 2011 by Steve Wellens
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid