Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

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

Open
CardenInsurance opened this issue May 8, 2023 · 0 comments
Open

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

CardenInsurance opened this issue May 8, 2023 · 0 comments
Labels
enhancement VB -> C# Specific to VB -> C# conversion

Comments

@CardenInsurance
Copy link

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 VB -> C#: Query syntax - incorrect Select statement added to LINQ group query #931 but I think it's a separate problem.
@CardenInsurance CardenInsurance added the VB -> C# Specific to VB -> C# conversion label May 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement VB -> C# Specific to VB -> C# conversion
Projects
None yet
Development

No branches or pull requests

2 participants