Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Learning to Like LINQ or, Loving the LINQ Loquacious

3.17/5 (3 votes)
30 May 2011CPOL2 min read 15.3K  
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:

Image 1

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:

SQL
select MyNumber from MyTable where MyNumber > 1

Here is a similar LINQ query:

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

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

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