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
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
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:
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:
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.
Subscribe to Blog updates
Thanks, we've got you!
Another Look into the Future with Rider’s Predictive Debugger
In the 2023.2 release cycle, we’ve introduced the Predictive Debugger in ReSharper, which gives you predictions about code paths and variables beyond the current execution pointer. We’ve written extensively about its advantages compared to alternative debugging strategies like thorough thinking, log…
Visualize Entity Framework Relationships and Additional Query Analysis in ReSharper 2023.3
A lot of teams are using Entity Framework or EF Core to work with their database. As an Object-Relational Mapper (ORM), it bridges objects in code to a relational database model, so that as a developer you don’t have to worry too much about the actual database. We all know: that’s not entirely tr…
Automatically Analyze ASP.NET Core Performance With Dynamic Program Analysis
Slow web pages may make your users or customers abandon your web application, even before they’ve had a proper look at it. You’ve likely also been frustrated working with a web application that is slow to load. The good news is that the latest versions of ReSharper and JetBrains Rider’s Dynamic P…
OSS Power-Ups: MassTransit – Webinar Recording
The recording of our webinar, OSS Power-Ups: MassTransit, with Chris Patterson, is available. This was the thirteenth episode of our OSS Power-Ups series, where we put a spotlight on open-source .NET projects. Subscribe to our community newsletter to receive notifications about future webinars.…