OrderBy not having any effect in LINQ

后端 未结 2 1197
遇见更好的自我
遇见更好的自我 2021-01-28 17:36

I have a table that has a set of data in it, as follows:

Notice the above results are gathered by the following SQL query:

select * from Logs wh         


        
相关标签:
2条回答
  • 2021-01-28 18:01

    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();
    
    0 讨论(0)
  • 2021-01-28 18:15

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

    0 讨论(0)
提交回复
热议问题