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

Query: OrderBy with Contains emits invalid SQL to the DB #7178

Closed
gdoron opened this issue Dec 3, 2016 · 3 comments
Closed

Query: OrderBy with Contains emits invalid SQL to the DB #7178

gdoron opened this issue Dec 3, 2016 · 3 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@gdoron
Copy link

gdoron commented Dec 3, 2016

var headerStories = await _context.Stories
            .Where(x => x.IsPublished)
            .Include(x => x.StoryCategories)
            .ThenInclude(x => x.Category)
            .ThenInclude(x => x.ParentCategory)
            .Include(x => x.Images)
            .Include(x => x.User)
            .OrderByDescending(x => headerIds.Contains(x.Id))
            .OrderByDescending(x => x.PublishDate)
            .Take(3)
            .AsNoTracking()
            .ToListAsync();

SQL:

SELECT TOP(@__p_1) [x].[Id], [x].[ActivityLocation], [x].[CanNotBeFeaturedOnHomePage], [x].[City], [x].[Country], [x].[GooglePlaceId], [x].[InsertDate], [x].[IsDeleted], [x].[IsProductsReviewed], [x].[IsPublished], [x].[LastUpdateDate], [x].[Latitude], [x].[LikesCount], [x].[Longitude], [x].[PostalCode], [x].[PublishDate], [x].[Quote], [x].[State], [x].[StreetName], [x].[StreetNumber], [x].[Title], [x].[UsedProducts], [x].[UserId], [x].[ViewsCount], [x].[YouTubeId], [u].[Id], [u].[AboutMe], [u].[AccessFailedCount], [u].[City], [u].[ConcurrencyStamp], [u].[Country], [u].[Email], [u].[EmailConfirmed], [u].[FirstName], [u].[HeaderImageUrl], [u].[InsertDate], [u].[InstagramUserName], [u].[LastName], [u].[Latitude], [u].[Location], [u].[LockoutEnabled], [u].[LockoutEnd], [u].[Longitude], [u].[NormalizedEmail], [u].[NormalizedUserName], [u].[PasswordHash], [u].[PhoneNumber], [u].[PhoneNumberConfirmed], [u].[PictureUrl], [u].[PostalCode], [u].[SecurityStamp], [u].[State], [u].[TwoFactorEnabled], [u].[UserName]
FROM [Stories] AS [x]
INNER JOIN [Users] AS [u] ON [x].[UserId] = [u].[Id]
WHERE [x].[IsPublished] = 1
ORDER BY [x].[PublishDate] DESC, [x].[Id] IN (379) DESC, [x].[Id]

EF Core version: (1.0.0) - sorry can't upgrade to 1.1.0 yet.
Operating system: Windows 10
Visual Studio version: (VS 2015)

I don't think the entire model classes are required, let me know if not.

Thanks.

@gdoron
Copy link
Author

gdoron commented Dec 3, 2016

p.s. I'm not looking for a workaround like splitting the query into two, as I already did that.
I'm just reporting the bug.

@rowanmiller rowanmiller added this to the 1.2.0 milestone Dec 12, 2016
@maumar
Copy link
Contributor

maumar commented Apr 13, 2017

This works in the current bits. We produce the following query (removed includes for simplicity):

SELECT TOP(@__p_1) [x].[Id], [x].[IsPublished], [x].[PublishDate]
FROM [Stories] AS [x]
WHERE [x].[IsPublished] = 1
ORDER BY [x].[PublishDate] DESC, CASE
    WHEN [x].[Id] IN (1, 2, 3)
    THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
END DESC

@maumar maumar closed this as completed Apr 13, 2017
@maumar maumar added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Apr 13, 2017
@gdoron
Copy link
Author

gdoron commented Apr 13, 2017

Thanks 🌷

@ajcvickers ajcvickers changed the title OrderBy with Contains emits invalid SQL to the DB Query: OrderBy with Contains emits invalid SQL to the DB May 9, 2017
@divega divega added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. labels May 10, 2017
@ajcvickers ajcvickers modified the milestones: 2.0.0-preview1, 2.0.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

5 participants