1

Given the same list, OrderBy and OrderByDescending on DateTime fields makes no diferent output:

List 1

List 2

"Fecha" field is DateTime.

This is the list:

ID  Movimiento_Anterior_ID  Cliente_ID  Fecha                    Monto      Tipo_Movimiento Venta_ID
6   3                       1020        2016-08-17 17:28:05.323  3940.000   NULL            NULL
5   3                       1020        2016-08-17 16:16:26.927  3817.000   NULL            NULL
4   3                       1020        2016-08-17 16:16:01.023  3694.000   NULL            NULL
3   -1                      1020        2016-08-17 16:15:23.937  3571.000   NULL            NULL
2   1                       15          2016-08-17 16:14:19.000  2091.000   NULL            NULL
1   -1                      15          2016-08-17 16:13:52.510  369.000    NULL            NULL
Slai
  • 22,144
  • 5
  • 45
  • 53
TestData
  • 23
  • 3

1 Answers1

2

Interestingly, I have no idea why, but the DefaultIfEmpty() is causing EF query provider to remove (ignore) the previous order by expressions from the generated SQL query. Weird behavior, but good to be known.

To get the intendended ordering, either remove DefaultIfEmpty() (I see no benefit of using it in such query), or move the OrderBy / OrderByDescending after the DefaultIfEmpty.

Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
  • It appears there used to be a Microsoft Connect issue about it, but it no longer is available. I found [this answer](http://stackoverflow.com/a/7615289/906773) which explains more. – Jesse Good Aug 17 '16 at 21:34
  • @JesseGood Good catch! Then I'm going to mark this as dupe. – Ivan Stoev Aug 17 '16 at 21:37