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()
    {
        listBusinessObject.Sort(
            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:

image

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

image

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

 

 

listBusinessObject.Sort(
            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 for the past 7 years. 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:


Comments

  1. Just like magic!

Your Comments

*

Protected with IP Blacklist CloudIP Blacklist Cloud

Follow

Get every new post delivered to your Inbox

Join other followers: