Rory Becker - DevExpress CodeRush Blog

CodeRush for Roslyn: ForEach to Linq

One really useful Refactoring provided by CodeRush for Roslyn is “ForEach to Linq

This refactoring is available whenever you have a ForEach that is iterating over a master list and attempting to select either a subset, or a single item, from that list.

Consider this legacy code example, where we have a list of orders and we want to extract a list of those orders which need Next-Day delivery.

ExtractPriorityOrdersForEach

Obviously that is fairly excessive. It’s also fairly complex to read.

  • There is a result set being prepared.
  • There is iteration over an incoming source set.
  • There is a condition being evaluated for each item in the source set.
  • There is the fact that passing items are being added to this result set.
  • Finally there is the returning of this result set at the end of the method.

So naturally we want to make this easier for you.

ForEach To Linq

So if you position your caret immediately left of the foreach, you will be able to select ForEach to Linq (Preview below)

ExtractPriorityOrdersForEachPreview

Once selected, this refactoring will reduce your unsightly loop to a simple LINQ Query.

ExtractPriorityOrdersPost

After this it’s pretty simple to reduce this to a single simple call.

ExtractPriorityOrdersForEachReduced

I think we can all agree that this is much easier on the eyes and on the brain as well.

Everything has been reduced to a simple LINQ query.

Depending on your needs you could change this function to return an IEnumerable<Order> and strip the ToList() off the end of the expression.
At that point you may even decide that the function, which previously hid your complexity, is now no longer necessary at all, and you can Inline and Delete this method.

Another example is with the following type of legacy search code:
Here we iterate over a list searching for the first item to match, in this case, on the basis of a name.

FindCustomerByNameForEachPre

The ForEach to Linq refactoring is again available from the foreach keyword.

FindCustomerByNameForEachPreview

Acceptance quickly removes the complexity of the original loop, replacing it with a simple Linq query.

FindCustomerByNameForEachPost

A quick Inline Temp later and the previously overly complex iteration and select, together with it’s Result variable has been completely eliminated in favour of a single statement which

FindCustomerByNameForEachTidy

In both of these examples, CodeRush has reduced the complexity of your code, helping you to replace old world manual loops with a concise expression of the results you want.

These are just a few of the ways that CodeRush for Roslyn can simplify your life.

If you don’t already have your copy of CodeRush for Roslyn, hop along to https://www.devexpress.com/products/coderush/ and pick up your free 30 day trial.
The paid version of CodeRush for Roslyn can be had for as little as $49. That is amazing value.

Published Feb 08 2017, 09:11 PM by
Bookmark and Share

Comments

Christian Peters

Any chance of refactoring to the method syntax?

February 8, 2017 11:27 PM

Rory Becker (DevExpress)

Hi Christian

Can I assume that you mean something like this ->

           return Orders.Where(o => o.Priority == PriorityEnum.NextDayGuarantee).ToList();

If so, then I think that would be a fine suggestion.

I'd recommend that you post it up on our support center (http://devexpress.com/sc ) so you can track progress.

February 9, 2017 3:12 AM

Christian Ista

Lambda expression (and more complex) are missing in the refactoring (see mail sent to mark few days ago)

February 9, 2017 6:24 AM

Juan Antonio Trujillo Montenegro

That's a nice feature but we would prefer to have it in Lambda instead of Linq.

February 11, 2017 7:46 AM

Rory Becker (DevExpress)

Hey Juan

Is that the same as Christian (Peters) was referring to (and I attempted to clarify) above.

February 13, 2017 6:04 AM
LIVE CHAT

Chat is one of the many ways you can contact members of the DevExpress Team.
We are available Monday-Friday between 7:30am and 4:30pm Pacific Time.

If you need additional product information, write to us at info@devexpress.com or call us at +1 (818) 844-3383

FOLLOW US

DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, ASP.NET, WinForms, HTML5 or Windows 10, DevExpress tools help you build and deliver your best in the shortest time possible.

Copyright © 1998-2017 Developer Express Inc.
All trademarks or registered trademarks are property of their respective owners