Entitty Framework外连接有效,直到我添加where子句

时间:2018-04-23 15:29:58

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

这有效:

  var query = from q in _context.Questions
                    join ua in _context.UserAnswers on q.ID equals ua.QuestionId
                    into temp
                    from ua in temp.DefaultIfEmpty()
                    select new ProfileViewModel
                    {
                        Question = new Question { ID = q.ID, Text = q.Text },
                        Answer = (ua == null ? "" : ua.AnswerText)
                    };

我收到了一个视图的五个空白userAnswer(有五个问题)的集合。太好了!

现在我需要添加条件来查找特定用户:

var query = from q in _context.Questions
                    join ua in _context.UserAnswers on q.ID equals ua.QuestionId
                    into temp
                    from ua in temp.DefaultIfEmpty()
                    where ua.User.Id == userId
                    select new ProfileViewModel
                    {
                        Question = new Question { ID = q.ID, Text = q.Text },
                        Answer = (ua == null ? "" : ua.AnswerText)
                    };

但现在集合中有ZERO项而不是5.

如何让它像第一个例子一样发送我的五个项目?

0 个答案:

没有答案