2012-12-20 80 views
0

使用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; } 
+0

你說你有三個entites的...請與我們分享了'Question'實體... –

+0

我增加了更多的細節質疑 – Chaka

回答

0

問題是,您從答覆中獲得問題,因此您只會收到有答覆的問題。如果你得到每一步的問題,然後回答每個問題的答案,它就應該起作用。

var steps = from s in db.Steps 
      orderby s.StepOrder 
      select new Quiz 
      { 
       StepID = s.StepID, 
       Title = s.Title, 
       Results = from question in s.Questions 
          orderby question.DisplayOrder        
          select new Result 
          { 
           QuestionID = question.QuestionID, 
           DisplayOrder = question.DisplayOrder, 
           Choices = question.Choices, 
           ControlType = question.ControlType, 
           QuestionText = question.QuestionText, 
           AnswerValue = question.Responses 
                .Where(r => r.ReviewID == id) 
                .Select(r => r.AnswerValue) 
                .FirstOrDefault() 
          } 
      }; 
0

不是很漂亮...但是它應該;)

var result = 
    from step in db.Steps 

    let questions = 
     db.Questions.Where(item => item.StepID == step.StepID) 
    let responses = 
     db.Responses.Where(item => 
       item.ReviewID == id 
       && questions.Any(q => q.QuestionID == item.QuestionID)) 

    select new Quiz 
    { 
     StepID = step.StepID, 
     Title = step.Title, 
     Results = 
      from question in questions.OrderBy(item => item.DisplayOrder) 
      select new Result 
      { 
       QuestionID = question.QuestionID, 
       DisplayOrder = question.DisplayOrder, 
       Choices = question.Choices, 
       ControlType = question.ControlType, 
       QuestionText = question.QuestionText, 
       AnswerValue = 
        responses.Any(item => item.QuestionID == question.QuestionID) 
        ? responses.First(item => 
         item.QuestionID == question.QuestionID).AnswerValue 
        : null 
      } 
    }; 
+0

謝謝你,我很欣賞的help..works太棒了! – Chaka