Rory Becker - DevExpress CodeRush Blog

February 2017 - Posts

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


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 or call us at +1 (818) 844-3383


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