LINQ, Lambda and the Learning Curve

3. January 2010

Been away for a while working on a 107,000 hour project. It isn?t done, but I am making time to blog and do my technical mentorship, because I have come to realize that there will always be high priority projects. You have to take the time to train and mentor during those projects because of that. Now on to the show.

I know that LINQ has received a massive welcome from the community and I have to say that it is a deserved on. On the whole it is a great addition to the language. I however don?t like one part of it. Verbose LINQ. I realize that it is more SQL like and that it is perceived to  shorten the learning curve, but I disagree. Lambdas are where the true power of LINQ sit and using verbose to me seems to focus you to learn LINQ one way so that you can re-learn it another.

I am going to focus on the Lambda side of LINQ simply out of preference. Keep an eye out here for the verbose VS. lambda death match, but that is a post for another time.

Lambda statements are actually fairly simple once you break them up.

SomeList.Where(c=>c.Id == someInt);

The above is the lambda conversion of this code block

foreach (Item item in SomeList)
{
    If(item.Id == soemInt)
        SomeOtherList.Add(item);
}

Lets break down the lambda so that this is a bit more clear.

c =>

This statement is declaring that the item in the list we are looking at is going to be held in the local variable named ?c?. This is no different than naming the variable in a for each loop ?c?. You can name your lambda variables more intuitively and I would encourage you to do so. Something like this.

SomeList.Where(menuItem => MenuItem.Id == someInt);

Once the object is declared you have the ability to use all of the members of that object. That is how we are able to use the ?Id? property to check something for a Boolean property. Selecting based on some Boolean expression is the most common use for Lambdas but not the only one.

List<SubMenu> submenus = SomeList.Select(menuItem => new SubMenu() 
                                                   {
                                                         ParentMenuId = menuItem.Id 
                                                   });

In this example we have created a submenu item from all the menu items in the list. We did this through the power of LINQ to let use change the output type of the selection at will. This is very usefull when you are creating items from other items. The code block that would represent is:

List<SubMenu> SubMenus = new List<SubMenu>();

foreach(MenuItem item in SomeList)
{
    SubMenu subMenu = new SubMenu();
    subMenu.ParentMenuId = item.Id;
    SomeOtherList.Add(subMenu);
}

To me this power is amazing. I have the ability to replace entire loops with a single line. The hit to readability is a concern but lambdas lend themselves to so many applications, and are key to LINQ so it should be part of the core toolset of a developer. If you use some sense and name your lambda variables intuitively you should get a lot of great millage out of LINQ without losing your readability.

C#, C# Helpful Functions, .NET

Comments

4/5/2010 8:54:02 AM #
Nice Post. It’s really a very good article. I noticed all your important points. Thanks!
4/5/2010 12:23:28 PM #
Thanks for sharing the information.It is definitely going to help me some time.
df
4/16/2010 12:12:18 PM #
hi..
4/16/2010 1:26:38 PM #
I was working on it since then on to build some effective codes then I go for another application called ASP.Net and that is what I’m working with right now. Thanks for sharing!
4/19/2010 2:07:24 AM #
My buddy and I were fighting about a dilemma similar to this! Now I know that I ended up being right. Many thanks for the facts you posted.
4/22/2010 1:01:34 AM #
I visit your web blog nearly every day and i like what are you doing with it. Many intresting articles and reviews on many hot topics and tendencies also, you have skills at writing. I constantly discover new things with your help and for that i say thank you with all my heart. Keep up this excellent work that you are accomplishing. Ok bye!
4/22/2010 8:32:21 AM #
Hi. this is kind of an -unconventional- question , but have other visitors asked you how get the menu bar to look like you've got it? I also have a blog and am really looking to alter around the theme, however am scared to death to mess with it for fear of the search engines punishing me. I am very new to all of this ...so i am just not positive exactly how to try to to it all yet. I'll just keep working on it one day at a time.
4/22/2010 4:07:07 PM #
I have forwarded this blog to my friends because it's very interesting.
4/23/2010 1:30:11 PM #
Good thing I have found this blog: I am going to read it carefully
4/24/2010 4:21:25 PM #
I was very pleased to find this site.I wanted to thank you for this great read I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.
4/25/2010 1:02:20 AM #
I have been surfing online more than three hours today, yet I never found any interesting article like yours. It's pretty worth enough for me. In my opinion, if all webmasters and bloggers made good content as you did, the internet will be much more useful than ever before.
4/25/2010 6:48:00 AM #
My friends will be happy when I will give them this address
4/26/2010 11:56:47 AM #
Nice information, many thanks to the author. It is incomprehensible to me now, but in general, the usefulness and significance is overwhelming. Thanks again for sharing this free online and good luck!
4/27/2010 12:39:34 AM #
Advantageously, the article is actually the greatest on this laudable topic. I fit in with your conclusions and will thirstily look forward to your future updates. Just saying thanks will not just be adequate, for the phenomenal clarity in your writing. I will at once grab your rss feed to stay privy of any updates. Pleasant work and much success in your  business endeavors!
4/27/2010 1:42:09 AM #
Advantageously, the post is in reality the sweetest on this valuable topic. I agree with your conclusions and will thirstily look forward to your upcoming updates. Saying thanks will not just be sufficient, for the tremendous clarity in your writing. I will directly grab your rss feed to stay privy of any updates. Pleasant work and much success in your business dealings!
4/27/2010 12:04:33 PM #
Can I make a suggestion?  I think youve got something good here.  But what if you added a couple links to a page that backs up what youre saying?  Or maybe you could give us something to look at, something that would connect what youre saying to something tangible?  Just a suggestion.
4/27/2010 12:04:55 PM #
Can I make a suggestion?  I think youve got something good here.  But what if you added a couple links to a page that backs up what youre saying?  Or maybe you could give us something to look at, something that would connect what youre saying to something tangible?  Just a suggestion.
4/28/2010 9:00:32 AM #
I\'m happy I found this blog, I couldnt discover any info on this subject matter prior to. I also run a site and if you want to ever serious in a little bit of guest writing for me if possible feel free to let me know, i\'m always look for people to check out my site. Please stop by and leave a comment sometime!
4/28/2010 1:38:45 PM #
When I originally commented I clicked the -Notify me when new comments are added- checkbox and now each time a comment is added I get four emails with the same comment. Is there any way you can remove me from that service? Thanks!
4/29/2010 1:56:04 AM #
Browsing a couple of blogengine.net blogs - it looks a remarkable platform. Best wishes to your website, cheers!
4/29/2010 6:13:44 AM #
We're a group of volunteers who are starting a brand new initiative in this community. Your The Devlineator | LINQ, Lambda and the Learning Curve gave us useful details. Thank you!
4/29/2010 12:05:26 PM #
We're a group of volunteers who are starting a brand new initiative in this community. Your The Devlineator | LINQ, Lambda and the Learning Curve gave us important data. Thank you very much!
4/30/2010 9:03:15 AM #
I am usually into blogging discovered this one. This great article makes so much. post on this website