MVC 3、EF4.1を使用して:
クイズ画面を作成し、ステップ、質問、応答の3つのエンティティに参加します
各ステップには多くの質問を含めることができ、各質問には1つまたはまったく回答を含めることができません
私の問題は、クエリに回答がない場合、質問のないステップが返されることです。このLINQに外部結合(左/右)を組み込むにはどうすればよいですか?
var steps = from s in db.Steps
join r in db.Responses.Where(x => x.ReviewID == id)
on s.StepID equals r.Question.StepID into g
orderby s.StepOrder
select new Quiz
{
StepID = s.StepID,
Title = s.Title,
Results = from x in g
orderby x.Question.DisplayOrder
select new Result
{
QuestionID = x.Question.QuestionID,
DisplayOrder = x.Question.DisplayOrder,
Choices = x.Question.Choices,
ControlType = x.Question.ControlType,
QuestionText = x.Question.QuestionText,
AnswerValue = x.AnswerValue
}
};
質問モデル:
public class Question
{
public Question()
{
this.Responses = new List<Response>();
}
public int QuestionID { get; set; }
public string QuestionText { get; set; }
public Nullable<bool> Required { get; set; }
public int DisplayOrder { get; set; }
public int StepID { get; set; }
public Nullable<int> DataType { get; set; }
public Nullable<int> ControlType { get; set; }
public string Choices { get; set; }
public Nullable<int> MaxLength { get; set; }
public virtual ICollection<Response> Responses { get; set; }
public virtual Step Step { get; set; }
public string NumberedQuestion
{
get { return String.Format("{0}. {1}", DisplayOrder, QuestionText); }
}
}
応答:
public class Response
{
public int ResponseID { get; set; }
public int UserID { get; set; }
public int QuestionID { get; set; }
public string AnswerValue { get; set; }
public int ReviewID { get; set; }
public virtual Question Question { get; set; }
}
手順:
public Step()
{
this.Questions = new List<Question>();
}
public int StepID { get; set; }
public int ReviewID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public int StepOrder { get; set; }
public virtual ICollection<Question> Questions { get; set; }