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.

5 comment(s)

Any chance of refactoring to the method syntax?

8 February, 2017

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.

9 February, 2017

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

9 February, 2017

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

11 February, 2017

Hey Juan

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

13 February, 2017

Please login or register to post comments.