Introducing ReSharper 5.0: Loops 2 LINQ

On the eve of ReSharper 5.0 going beta, we thought it’s just about time to start elaborating on new R# features. We’re hoping for a series of posts on improved code analysis, navigation to and within external sources, structural search & replace, ASP.NET support, and other stunning capabilities that ReSharper 5.0 provides. Today, we’ll talk about R# going crazy over LINQ.

Among many new code inspections introduced in ReSharper 5.0, two are specifically aimed at converting for and foreach loops into LINQ statements and/or combinations of extension methods and lambda expressions. Here they are:

  • Loop can be converted into LINQ-expression
  • Part of loop’s body can be converted into LINQ-expression

If you are wondering why you would ever transform traditional cozy loops to LINQ, Jimmy Bogard, Matthew Podwysocki, and Justin Etheredge have done a great job of explaining why.

Back to ReSharper work, let’s see how it converts loops to LINQ method or query syntax for you in common scenarios.

First, there’s the aggregation scenario where we loop through a collection in order to get the number of this collection’s items for which a certain condition holds true:

ReSharper highlights the foreach keyword with a green curly underline. You press Alt+Enter for available actions, and ReSharper suggests that you convert the loop to a LINQ expression:

You apply the conversion, and here we go: no more temporary counter variable. Instead, we’ve got Count extension method with a lambda-styled condition:

Next, we’re taking on the transformation scenario where we’re looping though a collection to populate another collection:

Again, ReSharper is smart enough to see that the code fragment can be converted to LINQ method syntax:

On the output side, we’ve got one line of code instead of three, thanks to the ToDictionary extension method:

For the next scenario, let’s take a collection and filter it out, so to speak, like this:

ReSharper encounters three foreach loops and readily suggests that we convert each of them. Because conversion only occurs within a selected loop and its nested loops, let’s press Alt+Enter at the top-level loop:

What we’ve got here is a pretty return statement that leverages LINQ query syntax:

All three examples above show how ReSharper converts entire loops to LINQ. In many cases, specifically with write usages inside loops, that can’t be done. For such occasions, ReSharper provides its second LINQ-related inspection: Part of loop’s body can be converted into LINQ-expression. Unlike the previous action, it’s displayed as a hint and highlighted with a straight green underline.

Let’s take another foreach loop with a complex condition and a write usage inside:

Pressing Alt+Enter at the foreach keyword highlighted as a hint lets you convert a part of the loop to LINQ syntax:

ReSharper integrates the condition with iteration variable definition, resulting in a LINQ query that makes the previously bloated write usage clear:

If, for any reason, you want to keep your loops for future generations and you don’t need LINQ-related code inspections, or, say, you want them to display as warnings or even errors, you can always configure them by choosing ReSharper | Options | Code Inspection | Severity.

The two loop 2 LINQ inspections and many more exciting features are available right now: we keep publishing fresh ReSharper 5 nightly builds that you can download and evaluate.

This entry was posted in How-To's, ReSharper Tips&Tricks and tagged , , , . Bookmark the permalink.

13 Responses to Introducing ReSharper 5.0: Loops 2 LINQ

  1. Pingback: Twitter Trackbacks for JetBrains .NET Tools Blog » Blog Archive » ReSharper 5.0 Preview: Loops 2 LINQ [jetbrains.com] on Topsy.com

  2. zvolkov says:

    Just be careful when the method yields values like so, if you convert this into LINQ, the “using” will exit prematurely.

    public IEnumerable Lookup(IEnumerable orderIDs)
    {
    using (session = SessionFactory.OpenSession())
    {
    foreach (var orderID in orderIDs)
    yield return session.Get(orderID )
    }
    }

    more details here: http://zvolkov.com/blog/post/2009/10/22/Returning-IEnumerable-from-using-statement.aspx

    http://zvolkov.com/blog/post/2009/10/22/Returning-IEnumerable-from-using-statement.aspx

  3. Pingback: popcyclical - Resharper 5 Beta Impressions

  4. Pingback: Visual Basic Tutorial: Visual Basic program to demonstrate looping … | VB WebDev Insider

  5. Pingback: JetBrains .NET Tools Blog » Blog Archive » Introducing ReSharper 5.0: Solution-Wide Warnings and Suggestions

  6. Pingback: JetBrains .NET Tools Blog » Blog Archive » Introducing ReSharper 5.0: Call Tracking

  7. Pingback: JetBrains .NET Tools Blog » Blog Archive » Introducing ReSharper 5.0: Value Tracking

  8. Pingback: JetBrains .NET Tools Blog » Blog Archive » Introducing ReSharper 5.0: Structural Search and Replace

  9. Julius says:

    What about an option to use Extension-based syntax and lambdas instead of LINQ statements? This would make it easier to convert a loop and immediately use extensions in other classes, for example.

  10. @Julius,

    Isn’t the aggregation scenario in this post what you’re talking about?

  11. Any plans on supporting this for VB.NET…

    The syntax for LINQ in VB.NET is ugly to say the least, but it would be very useful

    Please?

  12. @Marcus
    Yes we’re planning to provide loops-2-linq for VB.NET but apparently not for the early stages of EAP but rather sometime closer to release.

  13. Pingback: JetBrains .NET Tools Blog » Blog Archive » ReSharper 5.0 Is Out!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">