ToLambda Refactor From ReSharper is Just Too Cool!

Can hardly contain my excitement.  I was just cleaning up a business object to post in my MVC article and of course was using JetBrain’s Resharper 4.1.  Not sure if any of you remember, but when I started learning ASP.NET I developed some nice business objects to use with my MSDN articles.  One of the nice tricks used is using anonymous delegates to sort lists.  The code was a little tricky, but id did the job.  Here is the old code:

[DataObjectMethod(DataObjectMethodType.Select, true)]
    public List<BusinessObjectItem> GetMembers()
            new Comparison<BusinessObjectItem>(
                delegate(BusinessObjectItem lhs, BusinessObjectItem rhs)
                { return lhs.Name.CompareTo(rhs.Name); }));
        return listBusinessObject;

So, the first thing I did was to execute the ReSharper "cleanup" from the right mouse button as follows:


Then, when I hover over the "return" and press the right mouse button with the light bulb, I get this:


I say "Convert to Lambda" and I get this:



            new Comparison<BusinessObjectItem>(
                (lhs, rhs) => lhs.Name.CompareTo(rhs.Name)));
        return listBusinessObject;
Way to cool!  Just had to share.
About Peter Kellner

Peter is a software professional specializing in mobile and web technologies. He has also been a Microsoft MVP since 2007. In addition, he’s a multi-course video author at Pluralsight. To read more about Peter Kellner and his experience click here. For information about how Peter Kellner might be able to help you with your project click here.

Follow me:


  1. Just like magic!


Get every new post delivered to your Inbox

Join other followers: