我正在創建一個問卷畫面,我需要顯示帶有問題的部分以及用戶的回覆。這裏是我的模型:如何查詢有問題的部分及其答案?
** Section *****
public int SectionID { get; set; }
public string SectionText { get; set; }
** Question ****
public int QuestionID { get; set; }
public int SectionID { get; set; }
public string QuestionText { get; set; }
public bool Required { get; set; }
public int DisplayOrder { get; set; }
** Response ****
public int ResponseID { get; set; }
public int UserID { get; set; }
public int QuestionID { get; set; }
public string AnswerValue { get; set; }
public virtual Question Question { get; set; }
如何抓住這個直通LINQ或其他方式,顯示如下:
Section1: User Info
Question 1. Name: Bob Smith
Question 2. Phone: 999-999-9999
Section2: User Tasks
Question 1. Role: Engineer
Question 2. Location: Baltimore
我嘗試以下(DEOS不工作):
var sections = from b in db.Sections.Include(s => s.Questions.Select(q => q.Responses.Where(r => r.userId == 1))
orderby b.SectionOrder
select b;
與節你怎麼鏈接的問題? – Ulises
我剛剛更新了我的問題。 「SectionId」在與Section關聯的Question模型中缺失。 – Chaka
最後一個問題。我注意到,當沒有答案時,它不返回任何問題..有沒有相當於LINQ的左/右外連接?如果返回所有問題都返回? – Chaka