Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

VB -> C#: Error when converting LINQ expression #717

Closed
dfbag7 opened this issue Apr 8, 2021 · 1 comment
Closed

VB -> C#: Error when converting LINQ expression #717

dfbag7 opened this issue Apr 8, 2021 · 1 comment
Labels
VB -> C# Specific to VB -> C# conversion

Comments

@dfbag7
Copy link

dfbag7 commented Apr 8, 2021

Input code

    Sub Main()
        Dim arr(5) as Integer
        arr(0) = 0
        arr(1) = 1
        arr(2) = 2
        arr(3) = 3
        arr(4) = 4
        arr(5) = 5

        Dim r = From e In arr
                Select p = $"value: {e}"

        For each m In r
            Console.WriteLine(m)
        Next
    End Sub

Erroneous output

    static class Module1
    {
        public static void Main()
        {
            var arr = new int[6];
            arr[0] = 0;
            arr[1] = 1;
            arr[2] = 2;
            arr[3] = 3;
            arr[4] = 4;
            arr[5] = 5;
            ;
#error Cannot convert LocalDeclarationStatementSyntax - see comment for details
            /* Cannot convert LocalDeclarationStatementSyntax, System.InvalidCastException: Unable to cast object of type 'Microsoft.CodeAnalysis.CSharp.Syntax.EmptyStatementSyntax' to type 'Microsoft.CodeAnalysis.CSharp.Syntax.LocalFunctionStatementSyntax'.
               at System.Linq.Enumerable.<CastIterator>d__97`1.MoveNext()
               at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
               at ICSharpCode.CodeConverter.CSharp.MethodBodyExecutableStatementVisitor.<VisitLocalDeclarationStatement>d__31.MoveNext()
            --- End of stack trace from previous location where exception was thrown ---
               at ICSharpCode.CodeConverter.CSharp.HoistedNodeStateVisitor.<AddLocalVariablesAsync>d__6.MoveNext()
            --- End of stack trace from previous location where exception was thrown ---
               at ICSharpCode.CodeConverter.CSharp.CommentConvertingMethodBodyVisitor.<DefaultVisitInnerAsync>d__3.MoveNext()

            Input:

                    Dim r = From e In arr
                            Select p = $"value: {e}"

             */
            foreach (var m in r)
                Console.WriteLine(m);
        }
    }

Expected output

I believe the LINQ statement should be converted into something like this:

   var r = from e in arr
      let p = $"value: {e}"
      select p;

The reason is that, in VB.NET, the Select LINQ statement with assignments seems working like let in C# with following select of the variable defined in the let statement.

Details

  • Product in use: VS extension
  • Version in use: 8.2.2.0
  • Did you see it working in a previous version, which? Did not test in previous versions
@dfbag7 dfbag7 added the VB -> C# Specific to VB -> C# conversion label Apr 8, 2021
@GrahamTheCoder
Copy link
Member

Thanks, I hadn't seen this syntax before. The linq conversion code is more complicated than the rest of the converter, but I think this specific case could probably be added easily enough.

If anyone wants to have a go at this, as always, just run the above example under a debugger and see where it initially throws. Probably somewhere around here:

private async Task<CSSyntax.SelectClauseSyntax> ConvertSelectClauseSyntaxAsync(VBSyntax.SelectClauseSyntax vbSelectClause)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
VB -> C# Specific to VB -> C# conversion
Projects
None yet
Development

No branches or pull requests

2 participants