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

Linq to SQL does not take advantage of 'as' aliases #19489

Closed
GedasFX opened this issue Jan 6, 2020 · 2 comments
Closed

Linq to SQL does not take advantage of 'as' aliases #19489

GedasFX opened this issue Jan 6, 2020 · 2 comments

Comments

@GedasFX
Copy link

GedasFX commented Jan 6, 2020

Consider the following LINQ code:

            await _context.Users
                .Include(s => s.Categories)
                .Select(s => new
                {
                    Id = s.Id,
                    Name = s.Name,
                    PointsTotal = s.Categories.Sum(u => u.Points)
                })
                .OrderByDescending(e => e.PointsTotal)
                .ToListAsync();

A user has many categories where he can get points, and for leader board purposes I want to get the sum of the points he has.
Linq to SQL does translate the statement, however it is not optimal:
Provided:

      SELECT [s0].[Id] AS [Id], [s0].[Name] AS [Name], (
          SELECT SUM([s].[Points])
          FROM [UserCategories] AS [s]
          WHERE [s0].[Id] = [s].[UserId]) AS [PointsTotal]
      FROM [Users] AS [s0]
      ORDER BY (
          SELECT SUM([s1].[Points])
          FROM [UserCategories] AS [s1]
          WHERE [s0].[Id] = [s1].[UserId]) DESC

Expected:

      SELECT [s0].[Id] AS [Id], [s0].[Name] AS [Name], (
          SELECT SUM([s].[Points])
          FROM [UserCategories] AS [s]
          WHERE [s0].[Id] = [s].[UserId]) AS [PointsTotal]
      FROM [Users] AS [s0]
      ORDER BY [PointsTotal] DESC

Using Microsoft.EntityFrameworkCore.SqlServer 3.1.0 package

@ajcvickers
Copy link
Member

@maumar to look for duplicate or put on backlog.

@maumar
Copy link
Contributor

maumar commented Jan 7, 2020

dupe of #18775

@maumar maumar closed this as completed Jan 7, 2020
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants