question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Order By Date Descending Doesn't Return Correctly

See original GitHub issue

This 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:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
da3dsoulcommented, Aug 27, 2021

Yeah, I suppose. Give me a few to refactor it out and mock some data

1reaction
smitpatelcommented, Aug 27, 2021

Can you share a runnable repro code?

Read more comments on GitHub >

github_iconTop Results From Across the Web

LINQ OrderBy query fails to sort properly by descending ...
I have a simple linq query which sorts the records based on dateTime both descending and ascending. for some reason it's failing at...
Read more >
Sort by date not working correctly
i recently updated to windows 10 from 8.1 and the sort by date in the downloads folder is failing miserably, files that are...
Read more >
Sort Sheet Ascending/Descending in date column is not ...
When I go back to a backup file (saved 20/12/12) it doesn't have the problem — but then it lacks dozens of updates...
Read more >
How to sort by date in Excel: chronologically, by month, ...
You will learn how to quickly arrange dates in chronological order, sort by month ignoring years, sort birthdays by month and day, and...
Read more >
Excel Won't Sort Dates Correctly - The Solution! - YouTube
... dates in Excel, his spreadsheet wasn't working, and that however much he tried, Excel just wouldn't sort his dates by year correctly....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found