【问题标题】:How to use (left/right) outer join for this LINQ that join 3 entities?如何为这个连接 3 个实体的 LINQ 使用(左/右)外连接?
【发布时间】:2012-12-08 03:29:06
【问题描述】:

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

【问题讨论】:

  • 你说过你有三个实体...请与我们分享Question实体...
  • 我在问题中添加了更多细节

标签: asp.net-mvc linq entity-framework outer-join


【解决方案1】:

问题是您从回复中得到问题,所以您只得到有回复的问题。如果你得到每个步骤的问题,然后每个问题的响应,它应该可以工作。

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()
                          }
            }; 

【讨论】:

    【解决方案2】:

    不是很漂亮......但做了它应该做的;)

    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
                }
        };
    

    【讨论】:

      猜你喜欢
      • 2013-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      • 2014-02-24
      相关资源
      最近更新 更多