Lambda expressions are yet another one of those things that took me a while to really embrace/ understand but once I did, I use them exclusively in LINQ.
The reason it took me a while to finally start using them is because every time I tried to read an explanation of them, the author got bogged down in a ridiculous amount of details and explanations that I either got lost, or just lost interest. Once I learned how to use them, I found it was easier to go back and re-read those explanations and understand what all was going on. In other words, I learned them in reverse (Practice –> Theory)
So that’s what I’ll attempt to do today in this blog post. I want to at least get the possible readers understanding how to use them in practical situations, then they can go off and read about the theory of them and how they work, etc.
Here’s how a typical LINQ query looks like without using lambda expressions:
var query = from m in db.PersonalMessages
where m.ConversationID_FK == ConversationID
select m;
Okay, not bad. It looks like a SQL query in reverse (i.e. you start off with “from” and end with “select”). This is so that intellisense can help you out—if you started with select, it would have on idea where you were going with the query. But since you start with “from”, intellisense can do this:

Pretty cool. Okay, so going back to our query, we are selecting PersonalMessages where the ConversationID is equal to some value passed into the method. The query itself is kinda verbose, and I like to keep my code short and sweet, so how do I re-write this using lambda expressions? Here is the code, and then we’ll break it down:
var query = db.PersonalMessages
.Where(m => m.ConversationID_FK == ConversationID);
That’s pretty cool. We got rid of “select'”, “from” and “in”, etc. Cleaned it up quite a bit. What all is happening though? Basically it’s written out like:
My Query = Get Personal Messages from my data context Where the ConversationID is equal to this #
In other words, it reads exactly like it does in the first written out query, but it’s done faster. The where statement is like a mini-method. You’re defining some variable M (you can use any letter, I always use m for the sake of convention in my code) and then in this case you’re giving m some condition to work with.
You’re basically saying, define m real quick (m=>) as a stand-in for PersonalMessage. Then only return PersonalMessages where m’s ConversationID is equal to some value. Again, just to drill in the point, it’s like typing in:
var query = DataContext.PersonalMessages.Where(PersonalMessage=>PresonalMessage.ConvoID = ConvoID);
Now I want to get even faster with my method calling. Instead of defining a query, then returning it, or doing other things to it, I can easily chain things to it. Lets say I want to return a List of personal messages. I can now do:
List<PersonalMessage> myPersonalMessages = db.PersonalMessages
.Where(m => m.ConversationID_FK == ConversationID)
.ToList();
Awesome! Very easy. I can get even cooler now, all with the ease of dot notation. Lets say I want to orderit by the date the personal message was created. No problem, I can throw in an .OrderBy:
List<PersonalMessage> myPersonalMessages = db.PersonalMessages
.Where(m => m.ConversationID_FK == ConversationID)
.OrderBy(m=>m.DateCreated)
.ToList();
Again, I’m creating a little “mini-method” that is passing in the personal message as m, and saying to order it by the date it was created. The possibilities are limitless here, because I can keep using intellisense to guide me through methods and I can chain just about any LINQ method here:
In the above query, I threw in a “ThenBy” statement to order by the author’s name after I ordered it by the date it was created. I can do all these method calls very easily using Lambda expressions, all with the help of intellisense, and all without verbose query writing. Very cool!
Hope this was helpful to those like me that struggled understanding how to use Lambda expressions.
Cheers,
samer paul
Print | posted on Thursday, September 24, 2009 10:32 AM