这有效:
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.
如何让它像第一个例子一样发送我的五个项目?