OrderByDescending not working as expected

Viewed 87

I'm using EFCore and trying to get entities from the database sorted in DateTime order. The class looks like this, I googled and saw some other users had problems where they used string as Date but I'm using DateTime both in the DB and in C#.

public class MyEntity
{
  public int Id {get; set;}
  public int OwnerId {get;set;}
  public DateTime Modified {get;set;}
}

public IQueryable<MyEntity> Get(Expression<Func<MyEntity, bool>> expression) => _context.MyEntity.Where(expression);

List<MyEntity> myEntities = Get(x=> x.OwnerId == user.Id).OrderByDescending(x=> x.Modified).ToList();

And this is what I get out. The list is kinda in order but...

Modified= {1753-01-01 00:00:00} <--- 1753 to 2022-09-13 my first thought here was that maybe there is something special about 1753?
Modified= {2022-09-13 18:46:47} 
Modified= {2022-08-16 11:14:49} 
Modified= {2022-08-02 08:13:16} 
Modified= {2022-08-02 08:21:52} <- wrong time, times just seem to be in random order? My thought here was that perhaps orderbyDescending can't handle times?
Modified= {2022-08-02 08:58:30} 
Modified= {2022-08-02 09:13:07} 
Modified= {2022-08-02 11:15:30} 
Modified= {2022-08-02 11:50:43} 
Modified= {2022-08-01 07:05:52} 
Modified= {2022-08-01 12:52:09} 
Modified= {2022-08-01 13:38:59} 
Modified= {2022-07-20 18:56:32} 
Modified= {2022-07-14 10:53:09} < ... 07-19 to 07-14 to 07-18 Okay, here i'm lost. It's not 1753, it is not a time but a date that got out of order? Why?
Modified= {2022-07-18 15:00:02} 
Modified= {2022-07-17 14:28:52} 
Modified= {2022-07-15 15:13:53} 
Modified= {2022-07-15 15:03:47} 
Modified= {2022-07-15 13:39:31} 
Modified= {2022-07-15 12:37:53} 
Modified= {2022-07-15 13:45:41} 
Modified= {2022-07-15 12:39:37} 
Modified= {2022-04-08 08:05:22} 
Modified= {2022-04-04 08:53:45} 
Modified= {2022-04-04 08:34:12} 
Modified= {2022-04-04 13:19:56} 
Modified= {2022-04-04 14:16:25} 
Modified= {2022-04-04 14:37:36} 
Modified= {2022-04-01 13:48:00} 
Modified= {2022-04-01 12:44:28} 
Modified= {2022-04-01 11:58:12} 
Modified= {2022-03-17 09:22:58}  <--- 03-17 to 03-18 back to 03-17?
Modified= {2022-03-18 10:01:55} 
Modified= {2022-03-17 10:06:29} 
Modified= {2022-03-15 08:50:03} 
Modified= {2022-03-15 11:08:19} 
Modified= {2022-03-14 15:49:48} 

So what is the secret? what am I doing wrong?

1 Answers

It's difficult to know without a reproducible example, but I suspect the issue may be caused by (i) navigation properties if they exist on the MyEntities object or (ii) a data type issue on the server side. Try the following to debug each case:

(i) - Navigation Property Troubleshooting

Try selecting only the columns in the example prior to OrderByDescending as follows:


    List<MyEntity> myEntities = Get(x => x.OwnerId == user.Id)
        .Select(h => new { h.Id, h.OwnerId, h.Modified})
        .OrderByDescending(x => x.Modified)
        .ToList();

(ii) - Server-Side Data Type Troubleshooting

Try selecting putting .ToList() before .OrderByDescending() to pull the data down from the database and run the ordering in Linq on the client-side to determine if the behavior is consistent:


    var myEntities = Get(x => x.OwnerId == user.Id)
        .Select(h => new { h.Id, h.OwnerId, h.Modified})
        .ToList()
        .OrderByDescending(x => x.Modified);
Related