2012-12-20 2 views
0

, EF4.1 :
퀴즈 화면을 구축, 나는 세 단체에 가입하고 각 단계는 많은 질문을 할 수있는 단계, 질문, 응답3 개의 엔티티를 결합하는이 LINQ에 대해 (왼쪽/오른쪽) 외부 조인을 사용하는 방법? MVC 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

나는 감사합니다, 나는 help..works 큰 감사 – 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

에 의문을 제기하기 위해 더 많은 세부 사항을 추가! – Chaka

관련 문제