Roslyn: Convert foreach to Linq not suggested

Created on 4 Apr 2019  路  7Comments  路  Source: dotnet/roslyn

This applies to Visual Studio 2019 release.

I tried a simple example of convert foreach to Linq using the the following code

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        List<string> words = new List<string>( ) { "hello", "world " };

        foreach( string word in words )
        {
            System.Console.WriteLine( word );
        }
    }
}

Doing Ctrl dot on the foreach convert to Linq was not suggested, convert to for was. There is a comment on this issue https://developercommunity.visualstudio.com/content/problem/402010/convert-linq-to-foreach-is-not-always-suggested.html saying "Only simple expressions are allowed now." however the above example is about as simple as I can imagine.
So is there something else you have to do to switch this on or has it been removed?

_This issue has been moved from https://developercommunity.visualstudio.com/content/problem/513784/convert-foreach-to-linq-not-suggested.html

Area-IDE Concept-Continuous Improvement Developer Community IDE-CodeStyle help wanted

Most helpful comment

How do you expect that code to be converted to LINQ? If you're talking about .ForEach, then:

  1. that's not LINQ,
  2. using that method is often considered a bad practice.

All 7 comments

How do you expect that code to be converted to LINQ? If you're talking about .ForEach, then:

  1. that's not LINQ,
  2. using that method is often considered a bad practice.

The .ForEach is a method available for List but not for IEnumerable. It is not a LINQ method. We should not consider its conversion for Lists to avoid confusion for other collections.

The code sample does not have anything other than .ForEach to be converted to LINQ. As soon as you change it to something like

            foreach (string word in words)
            {
                if (word.StartsWith("h"))
                {
                    System.Console.WriteLine(word);
                }
            }

it would provide you a conversion suggestion for foreachand if.

This is still an issue (attempting to get the Convert To Linq to appear & work in the context menu when For Each is selected in the code editor) as of current production version of VS 2019 Community Edition 16.6.3.

https://docs.microsoft.com/en-us/visualstudio/ide/reference/convert-foreach-linq?view=vs-2019 indicates VB is supported.

C# currently does work. I haven't found a way to get VB to work with any For Each loop.

ConvertForEachToLinq-VB-00124

There is no VB implementation of this feature @elynch100 . We would take a community contribution to provide one if you're interested though!

So does the page need to be updated?
https://docs.microsoft.com/en-us/visualstudio/ide/reference/convert-foreach-linq?view=vs-2019

This refactoring applies to:
C#
Visual Basic

Seems like it. @mikadumont ?

I can go ahead and update the doc.

Was this page helpful?
0 / 5 - 0 ratings