SQL 查询到 LINQ for MVC,带有 SUM 和 NULL
本文关键字:带有 SUM NULL MVC 查询 LINQ for SQL | 更新日期: 2023-09-27 18:32:59
有人可以帮我将此查询转换为 LINQ 吗,因为我是使用 Linq 的新手,然后它将用于谷歌图表信息。
Select Question.SubSectionName, SUM(Answers.RatingAnswer) AS Ratings
FROM Question,Answers,Response,Section
Where Answers.QuestionID = Question.QuestionID
AND Answers.ResponseID = Response.ResponseID
AND Question.SectionID=Section.SectionID
AND Section.SectionID = 2
AND Response.ResponseID = @0
AND Question.SubSectionName IS NOT Null
GROUP BY Question.SubSectionName;
到目前为止,我得到了什么:
var submitted = (from ans in db.Answers join ques in db.Questions on
ans.QuestionID equals ques.QuestionID
join resp in db.Responses on ans.ResponseID equals resp.ResponseID
join sec in db.Sections on ques.SectionID equals sec.SectionID
where sec.SectionID == 2 && resp.ResponseID == model.ResponseID
&& ques.SubSectionName!= null
select ques.SubSectionName && ans.RatingAnswer)
感谢您的任何帮助。
根据您的评论,这应该输出一组 RatingAnswers 的总和:
var submitted =
(from ans in db.Answers
join ques in db.Questions on ans.QuestionId equals ques.QuestionId
join resp in db.Responses on ans.ResponseId equals resp.ResponseId
join sec in db.Sections on ques.SectionId equals sec.SectionId
where sec.SectionId == 2 && resp.ResponseId == model.ResponseID && ques.SubSectionName != null
select new { SubSectionName = ques.SubSectionName, RatingAnswer = ans.RatingAnswer })
.GroupBy(a => a.SubSectionName)
.Select(a => new { SectionName = a.Key, Sum = a.Sum(s => s.RatingAnswer) });
可能有更有效的方法来编写此内容。
我还要指出,对我来说,数据结构似乎有缺陷。也就是说,它似乎要么不完全规范化,要么不正确。这当然是你自己解决的。
>Linqer 可帮助您将 SQL 转换为 LINQ。
如果你想在 LINQ 中变得更好,我建议使用 LINQPad。但是,LINQPad 不能从 SQL 转换为 LINQ,而是从 LINQ 转换为 SQL。
试试这个-
from q in Question
join a in Answers on q.QuestionID equals a.QuestionID
join r in Response on r.ResponseID equals a.ResponseID
join s in Section on s.SectionID equals q.SectionID
where s.SectionID= 2 and r.ResponseID= @0 and q.SubSectionName!=null
Group by q.SubSectionName