使用MVC 3,EF4.1:
建設測驗屏幕,我加入三個實體:步驟,問題,響應如何使用(左/右)這個LINQ連接3個實體的外連接?
每一步都可以有很多問題,每一個問題可以有一個或沒有響應
我的問題是當我在查詢中沒有答案時,它返回沒有問題的步驟。如何將外連接(左/右)合併到此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; }
你說你有三個entites的...請與我們分享了'Question'實體... –
我增加了更多的細節質疑 – Chaka