Order By Date Descending Doesn't Return Correctly
See original GitHub issueThis could be a bug or a question.
Include your code
var query = images.Select(a => a.Image).ToQueryString();
int[] ExcludedTagIds = { 4416, 9868, 65, 9782, 7179, 6, 7178, 4399 };
var simplified = _context.Set<Image>()
.GroupJoin(_context.Set<ImageSource>(), image => image.ImageId,
source => EF.Property<int>(source, "ImageSourceId"), (image, source) => new { image, source })
.SelectMany(a => a.source.DefaultIfEmpty(), (a, b) => new { Image = a.image, PixivSource = b })
.Where(a => a.PixivSource.Source == "Pixiv" && a.Image.Tags.Any() &&
a.Image.Tags.All(b => !ExcludedTagIds.Contains(b.ImageTagId)))
.OrderByDescending(a => a.PixivSource.PostDate).Select(a => a.Image).Skip(0).Take(20);
query = simplified.ToQueryString();
var simpleResults = await simplified.ToListAsync();
query
evaluates to
.param set @__p_3 20
.param set @__p_2 0
SELECT "i"."ImageId", "i"."Height", "i"."ImportDate", "i"."Width"
FROM "Images" AS "i"
LEFT JOIN "ImageSources" AS "i1" ON "i"."ImageId" = "i1"."ImageSourceId"
WHERE ("i1"."Source" = 'Pixiv') AND ("i1"."PostDate" IS NOT NULL AND (EXISTS (
SELECT 1
FROM "ImageImageTag" AS "i2"
INNER JOIN "ImageTags" AS "i3" ON "i2"."TagsImageTagId" = "i3"."ImageTagId"
WHERE "i"."ImageId" = "i2"."ImagesImageId") AND NOT EXISTS (
SELECT 1
FROM "ImageImageTag" AS "i4"
INNER JOIN "ImageTags" AS "i5" ON "i4"."TagsImageTagId" = "i5"."ImageTagId"
WHERE ("i"."ImageId" = "i4"."ImagesImageId") AND "i5"."ImageTagId" IN (4416, 9868, 65, 9782, 7179, 6, 7178, 4399))))
ORDER BY "i1"."PostDate" DESC
LIMIT @__p_3 OFFSET @__p_2
The Query works. I run it on the DBMS (tried both SQLite and MSSQL), and it returns the correct results. I can be fine with the possible dupes here, as there will only ever be one Pixiv ImageSource per image.
What I get back from EF just doesn’t seem to match. The ordering is consistent, but consistently wrong, and I don’t see a pattern of any kind.
I might be doing something wrong. I got to this group join method in an attempt to avoid a subquery in the order by, as that tanks performance here. My instinct is that the final Select back into Image is wiping the ordering, but it’s translated to sql correctly (doesn’t return all of the ImageSource data), so I don’t know how I would handle that correctly. If this is the case, then a warning or something built into the EF analyzers would be very helpful diagnosing this, especially since the answer isn’t obvious to me, at least.
It’s a lot of effort to try to recreate this schema perfectly in an MVP, so I figured I’d hold off until told to do so.
Include stack traces
N/A
Include provider and version information
EF Core version: 6.0.0-preview.5.21301.9 (newest to target net5.0) Database provider: Same results on both SQLite and SqlServer Target framework: .NET 5.0 Operating system: linux/windows x64 (same results on both) IDE: Rider, but probably irrelevant here
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Yeah, I suppose. Give me a few to refactor it out and mock some data
Can you share a runnable repro code?