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.


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)


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


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


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.


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


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


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


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 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.

FREE DevExpress Xamarin UI Controls

Deliver high-impact native mobile apps with our feature-rich Data Grid, Scheduler, Chart, TabView, Editors, and utility controls. Reserve your free copy today!
Christian Peters
Christian Peters

Any chance of refactoring to the method syntax?

8 February 2017
Rory Becker (DevExpress)
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 ( ) so you can track progress.

9 February 2017
Christian Ista
Christian Ista

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

9 February 2017
Juan Antonio Trujillo Montenegro
Juan Antonio Trujillo Montenegro

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

11 February 2017
Rory Becker (DevExpress)
Rory Becker (DevExpress)

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.