因此,到目前为止,我已经尝试了几个小时,但仍然无法实现

表格是这样的。

**Questions**
   -  IDQuestion, IDSubject, DNI, others..
**Answers**
   - IDQuestion , IDAnswer, DNICreator, others..
**StudentsPersonalinformation (just to get names, nothing related to the answers)**
   - name, surname, DNI, others..


我想做的是在linq查询中获取问题的答案数量。我已经有了这个

var querySubjectQuestions = (from questions in db.questions
join studentspersonalinformation in db.studentspersonalinformation on questions.DNI equals studentspersonalinformation.DNI
where questions.IDSubject == IDSubject && questions.status == 1
select new
{
    IDQuestion = questions.IDQuestion,
    Title = questions.title,
    Date = questions.date,
    studentName = studentspersonalinformation.name,
    studentSurname = studentspersonalinformation.surname,
}).OrderByDescending(c => c.Date);


但是仍然不知道将.Count从另一个表放到哪里(IDQuestion等于它正在查询的那个)。

谢谢 :)

最佳答案

var querySubjectQuestions = (from questions in db.questions
                             join studentspersonalinformation in db.studentspersonalinformation on questions.DNI equals studentspersonalinformation.DNI
                             where questions.IDSubject == IDSubject && questions.status == 1
                             select new
                             {
                                 IDQuestion = questions.IDQuestion,
                                 Title = questions.title,
                                 Date = questions.date,
                                 studentName = studentspersonalinformation.name,
                                 studentSurname = studentspersonalinformation.surname,
                                 noAnswers = (from answer in db.answers
                                             where answer.IDQuestion == questions.IDQuestion)
                                             select answer).Count()
                              }).OrderByDescending(c => c.Date);

10-05 21:26