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

Redundant order by when selecting subquery field #27590

Closed
rofenix2 opened this issue Mar 8, 2022 · 1 comment
Closed

Redundant order by when selecting subquery field #27590

rofenix2 opened this issue Mar 8, 2022 · 1 comment

Comments

@rofenix2
Copy link

rofenix2 commented Mar 8, 2022

Hi, i don't know if this is intended behavior but when i try to order by a field that has a subquery, EFCore generates a copy of the subquery instead of using the field.

For example i have a "Post" table with "PostLike" table (1..N):

var test = (from post in Context.Set<Post>()
                 select new
                 {
                     Likes = post.Likes.Count
                 }).OrderByDescending(x => x.Likes);
var query = test.ToQueryString();

Gives me the following query:

SELECT (
    SELECT COUNT(*)
    FROM [Social].[PostLike] AS [p1]
    WHERE [p].[PostId] = [p1].[PostId]) AS [Likes]
FROM [Social].[Post] AS [p]
ORDER BY (
    SELECT COUNT(*)
    FROM [Social].[PostLike] AS [p0]
    WHERE [p].[PostId] = [p0].[PostId]) DESC

Instead of:

SELECT (
    SELECT COUNT(*)
    FROM [Social].[PostLike] AS [p1]
    WHERE [p].[PostId] = [p1].[PostId]) AS [Likes]
FROM [Social].[Post] AS [p]
ORDER BY Likes DESC

Is there any way to fix this?.
Currently using EFCore 7 preview 1.22076.6 with the same ver for SQL Server provider.

@maumar
Copy link
Contributor

maumar commented Mar 8, 2022

dupe of #18775

@maumar maumar closed this as completed Mar 8, 2022
@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