我有一个sql查询,它给了我正确的结果,即使用order by子句按A到Z排序的所有员工姓名
select Distinct(EmpNTLogin),Employee from Attendance
where CreateDate>='2016-01-01'
order by EmpNTLogin
当我在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();
在linq查询中,在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);
您需要首先应用"where"子句,然后应用"orderby"子句。类似这样的东西:
var query = (from attendance in db.Attendances
where attendance.CreateDate.Value.Year >= 2016
orderby attendance.EmpNTLogin
select new { attendance.Employee, attendance.EmpNTLogin }).Distinct();