efcore icon indicating copy to clipboard operation
efcore copied to clipboard

OrderByDescending(...).First() returns random for a not mapped property

Open gisek opened this issue 11 years ago • 9 comments

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

gisek avatar Oct 01 '15 22:10 gisek

Thanks for reporting.

anpete avatar Oct 01 '15 22:10 anpete

I think this is an issue with FirstOrDefault being translated to SQL even though the ordering is client-eval.

anpete avatar Oct 01 '15 22:10 anpete

@anpete That's also my guess.

gisek avatar Oct 01 '15 22:10 gisek

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

anpete avatar Oct 01 '15 22:10 anpete

Currently I'm totally out of time. :(

gisek avatar Oct 01 '15 22:10 gisek

No problem :smile:

anpete avatar Oct 01 '15 22:10 anpete

I'll try to fix it

felipepessoto avatar Oct 14 '15 22:10 felipepessoto

Is it fixed in this commit? https://github.com/aspnet/EntityFramework/blob/5fd6b00457b8b005cdedb6200de8c7989113221e/src/EntityFramework.Relational/Query/Internal/RelationalResultOperatorHandler.cs#L138

felipepessoto avatar Oct 24 '15 02:10 felipepessoto

Fixed

rowanmiller avatar Dec 09 '15 19:12 rowanmiller