我被困在下面的 sql 到 LINQ 查询的转换中。基本上我只想将我的考试分组在我的表成绩中,然后计算通过、失败、参加次数和通过率的总数
SELECT testID,
(SELECT COUNT(testID) FROM tblGrade AS b
WHERE b.testID= a.testID AND b.Grade < 80) AS 'Failed',
(SELECT COUNT(testID) FROM tblGrade AS b
WHERE b.testID= a.testIDAND b.Grade >= 80) AS 'Passed',
--taken = failed + passed,
--passingrate = (passed / taken) * 100
FROM dbo.tblGrade AS a
GROUP BY testID
ORDER BY testID
编辑:我的解决方案如下:它有效,但我认为它不是最好的方法,尤其是失败和传递的属性。
var xx1 = _unitOfWork.tblGrade.GetAll().GroupBy(a => new { a.testID});
var xx2 = xx1.Select(b => new
{
testID= b.Key.testID,
failed = _unitOfWork.tblGrade.Query(filter: a => a.testID == b.Key.testID).Where(c => c.Grade < 80).Count(),
passed = _unitOfWork.tblGrade.Query(filter: a => a.testID == b.Key.testID).Where(c => c.Grade >= 80).Count(),
//taken = failed + passed,
//passingrate = (passed / taken) * 100
}).ToList();