为什么我的Linqued查询没有给我正确的结果?

aka*_*ash 2 linq sql-server linq-to-sql sql-to-linq-conversion

我有这个sql查询,它给了我正确的结果,即使用order by子句按A到Z排序的员工的所有名称

select Distinct(EmpNTLogin),Employee from Attendance 
where  CreateDate>='2016-01-01'
order by EmpNTLogin
Run Code Online (Sandbox Code Playgroud)

当我在Linq中转换相同的查询时,我得到了正确的结果,但order by子句不起作用.这是我的Linqued查询

    var query = (from attendance in db.Attendances
                     orderby attendance.EmpNTLogin
                     where attendance.CreateDate.Value.Year >= 2016
                     select new { attendance.Employee, attendance.EmpNTLogin }).Distinct();
Run Code Online (Sandbox Code Playgroud)

Sil*_*ind 5

在linq查询中,在之后应用distinct orderby,因此丢弃该顺序.

在调用distinct之后应用orderby

var query = (from attendance in db.Attendances
             where attendance.CreateDate.Value.Year >= 2016
             select new
             {
                 attendance.Employee,
                 attendance.EmpNTLogin
             }).Distinct().OrderBy(att => att.EmpNTLogin);
Run Code Online (Sandbox Code Playgroud)