Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / C#

Learning to Like LINQ or, Loving the LINQ Loquacious

Rate me:
Please Sign up or sign in to vote.
3.17/5 (3 votes)
30 May 2011CPOL2 min read 14.9K   5   6
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)


Written By
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 Pin
Shashank Bisen1-Jul-11 18:20
Shashank Bisen1-Jul-11 18:20 
Generalinteresting Pin
BillW3317-Jun-11 7:31
professionalBillW3317-Jun-11 7:31 
GeneralMy vote of 3 Pin
Qwertie7-Jun-11 9:39
Qwertie7-Jun-11 9:39 
GeneralReverse Polish SQL LINQ explination [modified] Pin
JV99992-Jun-11 20:42
professionalJV99992-Jun-11 20:42 
GeneralRe: Reverse Polish SQL LINQ explination Pin
Steve Wellens3-Jun-11 3:35
Steve Wellens3-Jun-11 3:35 
GeneralRe: Reverse Polish SQL LINQ explination Pin
JV99993-Jun-11 5:30
professionalJV99993-Jun-11 5:30 

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.