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

Normalize .Count != 0 to Any() in preprocessing #33644

Closed
roji opened this issue Apr 30, 2024 · 0 comments · Fixed by #33654
Closed

Normalize .Count != 0 to Any() in preprocessing #33644

roji opened this issue Apr 30, 2024 · 0 comments · Fixed by #33654
Assignees
Labels
area-perf area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement

Comments

@roji
Copy link
Member

roji commented Apr 30, 2024

The following two queries:

_ = await context.Blogs.Where(b => b.Posts.Any()).ToListAsync();

_ = await context.Blogs.Where(b => b.Posts.Count > 0).ToListAsync();

... translate to very different SQLs:

SELECT [b].[Id]
FROM [Blogs] AS [b]
WHERE EXISTS (
    SELECT 1
    FROM [Post] AS [p]
    WHERE [b].[Id] = [p].[BlogId])

SELECT [b].[Id]
FROM [Blogs] AS [b]
WHERE (
    SELECT COUNT(*)
    FROM [Post] AS [p]
    WHERE [b].[Id] = [p].[BlogId]) > 0

We should normalize the latter (Count > 0, Count != 0) to the former (Any). This is important especially as VS suggests changing Any() to Count > 0.

Analyzed and flagged by @TehWardy in #33638

@roji roji self-assigned this Apr 30, 2024
roji added a commit to roji/efcore that referenced this issue May 1, 2024
roji added a commit to roji/efcore that referenced this issue May 1, 2024
@roji roji added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label May 1, 2024
@roji roji added this to the 9.0.0 milestone May 1, 2024
@ajcvickers ajcvickers modified the milestones: 9.0.0, 9.0.0-preview5 May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-perf area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants