CodeConverter icon indicating copy to clipboard operation
CodeConverter copied to clipboard

VB -> C#: LINQ orderby and select are in wrong order

Open CardenInsurance opened this issue 1 year ago • 0 comments

VB.Net input code

Public Class ConversionTest7
    Private Class MyEntity
        Property EntityId As Integer
        Property FavoriteColorId As Integer
        Property Name As String
    End Class
    Private Class MyColor
        Property ColorId As Integer
        Property Color As String
    End Class
    Public Sub BugRepro()

        Dim entities As New List(Of MyEntity)
        Dim colors As New List(Of MyColor)

        Dim wrongOrder = (From e In entities
                          Join c In colors On c.ColorId Equals e.FavoriteColorId
                          Where e.FavoriteColorId > 0 And c.Color = "green"
                          Select e, c
                          Order By e.Name).ToList
    End Sub
End Class

Erroneous output

public class ConversionTest7
    {
        private class MyEntity
        {
            public int EntityId { get; set; }
            public int FavoriteColorId { get; set; }
            public string Name { get; set; }
        }
        private class MyColor
        {
            public int ColorId { get; set; }
            public string Color { get; set; }
        }
        public void BugRepro()
        {

            var entities = new List<MyEntity>();
            var colors = new List<MyColor>();

            var wrongOrder = (from e in
                                  from e in entities
                                  join c in colors on e.FavoriteColorId equals c.ColorId
                                  where e.FavoriteColorId > 0 & c.Color == "green"
                                  select new { e, c }
                              orderby e.Name
                              select e).ToList();
        }
    }

Expected output


var wrongOrder = (from e in entities
                              join c in colors on e.FavoriteColorId equals c.ColorId
                              where e.FavoriteColorId > 0 & c.Color == "green"
                              orderby e.Name
                              select new { e, c }).ToList();

Details

  • Product in use: VS extension
  • Version in use: 9.2.2.0
  • Even though it's bad practice, Visual Studio allows the OrderBy to be after the Select when writing VB.NET. But after converting to C#, VS shows an error with the code. The converter should swap the OrderBy and Select statements in this example to prevent the error.
  • The converted code above also illustrates issue #931 but I think it's a separate problem.

CardenInsurance avatar May 08 '23 18:05 CardenInsurance