LinQ OrderBy和OrderByDescending在DateTime字段中不起作用

时间:2016-08-17 20:58:40

标签: c# linq datetime

给定相同的列表,DateTime字段上的OrderBy和OrderByDescending不会产生不同的输出:

列表1

列表2

“Fecha”字段为DateTime

这是清单:

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

1 个答案:

答案 0 :(得分:2)

有趣的是,我不知道为什么,但DefaultIfEmpty()导致EF查询提供程序通过生成的SQL查询中的表达式删除(忽略)先前的顺序。奇怪的行为,但很高兴知道。

要获得有意义的排序,请移除DefaultIfEmpty()(我认为在此类查询中没有使用它的好处),或移动{/ 1}} / OrderBy > OrderByDescending