请 - 我有这个SQL语句:
SELECT FK_ClassId, LectureDays
FROM tbl_TimeTables
WHERE Term = 'First Term'
AND FK_session = 4
AND fk_classId = 1
AND (fk_subjectid <> 1)
GROUP BY FK_classId, LectureDays
HAVING (COUNT(*) < 6)
返回此结果:
Image Embedded Here, giving the right result
但是当我向linq解释时,我得到了不同的结果:
Tbl_TimeTables.GroupBy(x => new { x.FK_Session, x.Term, x.LectureDays,
x.FK_ClassId, x.FK_SubjectId })
.Where(grp => grp.Count() < 6 && grp.Key.FK_Session == 4 && grp.Key.Term ==
"First Term" && grp.Key.FK_ClassId == 1 && grp.Key.FK_SubjectId != 1)
.Select(grp => new
{
LectureDay = grp.Key.LectureDays,
ClassId = grp.Key.FK_ClassId
})
Wrong Results Picture Link here
请查看我的代码,我做错了什么?
由于
添
答案 0 :(得分:1)
根据Matt Gibson的建议,这是linq查询的正确方法:
Tbl_TimeTables
.Where(x => x.FK_Session == 4 && x.Term == "First Term" && x.FK_ClassId == 1
&& x.FK_SubjectId != 1)
.GroupBy(x => new { x.FK_ClassId, x.LectureDays })
.Where(grp => grp.Count() < 6)
.Select(grp => new
{
ClassId = grp.Key.FK_ClassId,
LectureDay = grp.Key.LectureDays
})
这与sql完全一样 还要指出这个链接:http://www.dotnettricks.com/learn/sqlserver/definition-use-of-group-by-and-having-clause帮助我理解了语句的工作方式,这有助于了解Matt在说什么。