OrderBy not having any effect in LINQ

泪湿孤枕 提交于 2019-12-02 12:26:30

As @Ivan Stoev already mentioned: OrderBy before Distinct / GroupBy is ignored by LINQ to Entities sql translator.

But if you must use it, use it before the OrderBy

 var logs = db.Logs.Where(x => x.RegisterationId == EnrollNumber && x.Date >= 
                    StartDate && x.Date <= EndDate && x.isIgnore != true).Distinct().OrderBy(x => x.DateTime).ToList();

Excluding the Distinct() should give you the appropriate list:

var logs = db.Logs.Where(x => x.RegisterationId == EnrollNumber && x.Date >= 
                   StartDate && x.Date <= EndDate && x.isIgnore != true).OrderBy(x => x.DateTime).ToList();

.Distinct destroys order. Switch the position of the Distinct and the OrderBy calls.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!