将 mvc 3 与 EF 4.1 一起使用(带有存储过程)。我有一个问题列表,我需要包含一个对每个问题的评论列表。不确定如何在一个过程和一个模型中完成此操作,而无需遍历每个问题模型并添加注释。这是我到目前为止所拥有的:(总之需要一个更好的方法)
我的程序“SP_GetQuestionIssues”:
CREATE PROC [dbo].[SP_GetQuestionIssues]
(@ReviewID int)
AS
Select r.ReviewID, q.QuestionId, s.Title Step, q.QuestionText Question, r.AnswerValue Answer, IsIssue
From dbo.Questions q
Join dbo.Steps s
on q.StepId = s.StepId
Join dbo.QuestionFlagged qf
on q.questionId = qf.questionId
Join dbo.Responses r
on q.questionId = r.QuestionID
Where IsIssue=1
AND r.ReviewID = @ReviewID
AND qf.ReviewID = @ReviewID
Order by s.StepID, s.stepOrder, q.DisplayOrder
我的模型“问题”:
public class QuestionIssue
{
public int ReviewID { get; set; }
public int QuestionId { get; set; }
public string Step { get; set; }
public string Question { get; set; }
public string Answer { get; set; }
public bool IsIssue { get; set; }
public virtual IEnumerable<Comment> Comments { get; set; }
}
我的模型“评论”:
public class Comment
{
public int CommentId { get; set; }
public int ReviewId { get; set; }
public Nullable<int> QuestionId { get; set; }
public string Reviewer { get; set; }
public string CommentText { get; set; }
public bool IsMemo { get; set; }
}
我的行动:
SqlParameter param = new SqlParameter("@ReviewID", Id);
var issues = db.Database.SqlQuery<QuestionIssue>("SP_GetQuestionIssues @ReviewID", param).ToList();
需要比这更好的方法(伪代码):
foreach(var issue in issues)
{
var comments = somefunction.getcomments(issue.reviewId, issue.questionId)
issue.Comments.add(comment)
}