【问题标题】:How can I get questions with answers?我怎样才能得到有答案的问题?
【发布时间】:2021-10-10 06:40:49
【问题描述】:

我有两个模型问题和答案

public class Question
    {
        public int QuestionId { get; set; }
        public string QuestionContent { get; set; }
        public List<Answer> Answers { get; set; }

        public int QuizId { get; set; }
        public Quiz Quiz { get; set; }
    }

public class Answer
    {
        public int AnswerId { get; set; }
        public string AnswerContent { get; set; }
        public bool IsCorrect { get; set; }

        public int QuestionId { get; set; }
        public Question Question { get; set; }

    }

我需要获取答案列表。我可以在测验中获得问题列表,但我不知道如何获得答案。如果与数据库设计有任何关系,我也很乐意得到一些帮助。提前致谢

【问题讨论】:

  • 根据问题指南,请展示您的尝试并告诉我们您发现了什么(在本网站或其他地方)以及为什么它不能满足您的需求。
  • 我曾经收到过uow.Questions.GetAll().Where(x=&gt;x.QuizId == id) 之类的问题,实际上我还没有发现任何类似的问题。我只看到过这个stackoverflow.com/questions/23677003/…,但这不是在我的情况下存储此类数据的正确方法

标签: asp.net sql-server entity-framework-core


【解决方案1】:

您可以通过以下方式通过特定的QuizId获得答案:

uow.Answers.GetAll().Were(x => x.Question.QuizId == id)

或者如果你想把所有东西放在一起:

uow.Quizes.GetAll().Where(x => x.Id == id).Include(x => x.Questions).ThenInclude(x => x.Answers)

假设GetAll() 返回IQueryable

【讨论】:

  • 谢谢,我稍后再试,如果有效,我会将您的答案标记为正确:)
猜你喜欢
  • 2018-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多