【问题标题】:Get null for empty collection in LINQ Group为 LINQ 组中的空集合获取 null
【发布时间】:2018-08-04 02:28:28
【问题描述】:

我正在研究 LINQ 查询,部分目标是执行一次 SQL 数据库调用以取得结果。我有很多问题可能有答案。

我需要选择所有问题和答案集合,如果没有特定问题的答案,我仍然需要它。

代码只给我有答案的问题,而不是没有答案的问题

var t3 = (Context.Answers
    .Include(answer => answer.AnswerStatusType)
    .Where(answer => Context.Questions.Where(q => q.profileId == ProfileId)
    .Any(t => t.Id == answer.QuestionId)))
    .GroupBy(
        x => x.QuestionId,
        x => x,
        (key, g) => new
        {
            Question = key,
            Answers = g.ToList(),
        }
    ).ToList();

【问题讨论】:

  • Context.Questions 开头,然后...
  • 如果你使用EF,你不应该有一个从QuestionsAnswers的导航属性吗?如果不是,您不应该使用联接吗?
  • 如何添加加入,考虑到我还需要没有任何答案的问题列表

标签: linq .net-core entity-framework-core


【解决方案1】:

你可能需要这样的东西:

var t3 =
(
    from q in Context.Questions
    where q.profileId == ProfileId
    join a in Context.Answers on q.Id equals a.QuestionId into gas
    select new
    {
        Question = q,
        Answers = gas.ToList(),
    }
).ToList();

【讨论】:

    【解决方案2】:

    我遇到了完全相同的问题,并用这个巨大的查询解决了它,它可能会被优化,但也许它会帮助有类似问题的人。

            // Perform left join to get all the questions (even those that don't have answers)
            var results = (from quiz in db.Quizzes
                          join question in db.Questions on quiz.QuestionID equals question.QuestionID
                          from answer in db.QuestionAnswers.Where(a => question.QuestionID == a.QuestionID).DefaultIfEmpty()
                          where quiz.QuizID == quizId
                          group (answer != null ? new AnswerVM()
                          {
                              AnswerId = answer.AnswerID,
                              AnswerText = answer.AnswerText,
                              Correct = question.CorrectAnswerID != null ? answer.AnswerID == question.CorrectAnswerID : false,
                          } : null) // For the questions with no answers 
                          by new
                          {
                              question.QuestionID,
                              question.DisplayOrder,
                              question.QuestionText,
                              question.CorrectAnswerID
                          } into g
                          select new QuestionVM
                          {
                              QuestionId = g.Key.QuestionID,
                              QuestionText = g.Key.QuestionText,
                              CorrectAnswerId = g.Key.CorrectAnswerID,
                              Answers = g.ToList()
                          })
                    .AsEnumerable()
                    .Select(x => {
                        // If there are no answers then group by will return the list with one value (null), 
                        // then we need to change the answers list to be null (not the list with one null value)
                        if (x.Answers.Count(a => a != null) <= 0)
                        {
                            x.Answers = null;
                        }
                        return x;
                    }).ToList();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-20
      • 2022-01-11
      相关资源
      最近更新 更多