Linq to entity - 基于孩子的孙子女排除孩子

时间:2015-06-04 09:28:03

标签: c# linq lambda entity-framework-6

我需要帮助解决一个我无法理解的问题。我想收到一份问卷,其中只包含未回答的问题。

要明确的是,一个人应该能够提出问卷,回答一个问题,比方说,共计5个问题。然后,当下次要求问卷时,应该返回,但只有4个问题(未答复的问题)。

我尝试使用lambda包括但不能让它工作。任何帮助真的很感激!

这些类的结构类似于

public class Questionnaire()
{
    public int Id {get; set;}
    public List<Question> {get; set;}
}

public class Question()
{
    public int Id {get; set;}
    public int QuestionnaireId {get; set;}
    public List<Answer> {get; set;}
}

public class Answer()
{
    public int Id {get; set;}
    public int QuestionId {get; set;}
    public int QuestionnaireId {get; set;}
    public List<ReceivedAnswer> {get; set;}
}

public class ReceivedAnswer()
{
    public int AnswerId {get; set;}
    public int QuestionId {get; set;}
    public int QuestionnaireId {get; set;}
    public int PersonId {get; set;}
}

1 个答案:

答案 0 :(得分:0)

假设你有桌子:

Questionnaires[QuestionnairesID, QuestionID]
ReceivedAnswers[PersonID , AnswerID]
Questions[QuestionID,Question]
Answers[AnswerID,QuestionID,Answer]

Linq Query就像:

var pp = (from q in questionnaire.Questions
         join a in question.Answers on q.questionid equals a.questionid
         join ra in ReceivedAnswers on ra.answerid equals a.answerid
         where ra.personid == inPersonID
         select q.questionid).ToList();

var output = from questionnaire in _unitOfWork.Questionnaires
        where questionnare.Id == questionnaireId && !pp.contains(q.questionid)
        select questionnaire