错误的写法:

public static List<Answer> GetAnswer_ByQuestionID(int questionID)
{
  using (IA2SContext context = new IA2SContext())
{ var list = (from o in context.Answer where o.QuestionID == questionID select o).ToList(); return list; } }

解决方案:(.ToList<Answer>())

public static List<Answer> GetAnswer_ByQuestionID(int questionID)
{
   using (IA2SContext context = new IA2SContext())
     {
          var list = (from o in context.Answer
              where o.QuestionID == questionID
              select o).ToList<Answer>();
          return list;
     }
}

第二种就是查询的时候包含另一张表:

解决方案:(.Include("Answer"))

public static List<Question> GetQuestionList_ByCategoryID(int categoryID)
{
      using (IA2SContext context = new IA2SContext())
      {
           var list = (from o in context.Question.Include("Answer")
               where o.CategoryID == categoryID && o.IsActive == true
               select o).OrderBy(p => p.SortOrder).ToList<Question>();
           return list;
          }
     }
}

 

相关文章:

  • 2021-05-16
  • 2021-07-24
  • 2021-10-22
  • 2022-02-01
  • 2021-10-18
猜你喜欢
  • 2021-07-24
  • 2021-07-21
  • 2022-12-23
  • 2022-12-23
  • 2021-09-17
  • 2021-07-06
相关资源
相似解决方案