LINQ to SQL does not generate ORDER BY when DISTINCT is used?

陌路散爱 提交于 2019-11-27 22:16:37

That is a limitation of SQL and Relational Algebra of where the ORDER BY is in relation to the DISTINCT.

The ORDER BY must be "further out" in the SQL (at the "top level") since it's a view operation. While one can write SQL that has the ORDER BY "further in", in relationship to a RA operation, it often results in Undefined Behavior (that sometimes works). In this light it makes sense that Linq2Sql is free to ignore the ORDER BY although, perhaps an exception would be better... it would be less subtle anyway ;-) (Actually, this same issue exists for any Linq provider that does not provide a "stricter" definition of Distinct.)

Remove the Distinct() and the Linq2Sql should once again generate the ORDER BY as expected. The solution is just to switch the order of operations so the ORDER BY is once again at the "top level".

This is covered by the article Use of Distinct and OrderBy in LINQ:

This behavior might appear strange. The problem is that the Distinct operator does not grant that it will maintain the original order of values. Applied to LINQ to SQL, this mean that a sort constraint can be ignored in the case of a query like queryA.

The solution is pretty s[i]mple: put the OrderBy operator after the Distinct one, like in the following queryB definition:

var queryB = 
    (from o in db.Orders
     select o.Employee.LastName)
    .Distinct().OrderBy( n => n );

Happy coding.

I got same issue while sorting year from transaction table.

try this

 var results = (from stats in db.t_harvest_statistics
                       select stats.unit_number).Distinct().OrderBy(x =(Int16)x.unit_number).ToList();

after getting distinct value use orderby method

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