0

使用 MVC 3,EF4.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; }
4

2 回答 2

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
            }
    };
于 2012-12-20T15:46:24.367 回答
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()
                          }
            }; 
于 2012-12-20T17:16:58.913 回答