Skip to content

ToLambda Refactor From ReSharper is Just Too Cool!

Updated: at 11:17 PM

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.