OrderByDescending(...).First() returns random for a not mapped property
For an unmapped property:
public DateTime TimeStampInternal { get; set; }
[NotMapped]
public Instant TimeStamp
{
get { return TimeStampInternal.AsUtc().ToInstant(); }
set { TimeStampInternal = value.ToDateTimeUtc(); }
}
This code returns a random value
var latestSnapshot = _context
.TagsSnapshots
.Include(snapshot => snapshot.Tags)
.Where(snapshot => snapshot.SiteId == siteId)
.OrderByDescending(snapshot => snapshot.TimeStamp)
.FirstOrDefault();
This one however works fine
var latestSnapshot = _context
.TagsSnapshots
.Include(snapshot => snapshot.Tags)
.Where(snapshot => snapshot.SiteId == siteId)
.OrderByDescending(snapshot => snapshot.TimeStamp)
.ToList()
.FirstOrDefault();
The same with First, FirstAsync, FirstOrDefaultAsync
Thanks for reporting.
I think this is an issue with FirstOrDefault being translated to SQL even though the ordering is client-eval.
@anpete That's also my guess.
If you want to send a PR, the issue is likely around this line:
https://github.com/aspnet/EntityFramework/blob/dev/src/EntityFramework.Relational/Query/Internal/RelationalResultOperatorHandler.cs#L141
Currently I'm totally out of time. :(
No problem :smile:
I'll try to fix it
Is it fixed in this commit? https://github.com/aspnet/EntityFramework/blob/5fd6b00457b8b005cdedb6200de8c7989113221e/src/EntityFramework.Relational/Query/Internal/RelationalResultOperatorHandler.cs#L138
Fixed