MVC 视图模型和 linq 查询

MVC viewmodel and a linq query

我终于(在 4 周后)得到了 MVC5 项目的结果,并且进展顺利。 现在我正在尝试 "limit" 结果的数量并且它正在标记错误:

'Stoopid' is a type, which is not valid in the given context
'Student' is a type, which is not valid in the given context

这是模型:

namespace viewModelA
{
    public class Teacher
    {
        public int TeacherId { get; set; }
        public string Code { get; set; }
        public string Name { get; set; }
    }
    public class Student
    {
        public int StudentId { get; set; }
        public string Code { get; set; }
        public string Name { get; set; }
        public string EnrollmentNo { get; set; }
    }
    public class Stoopid
    {
        [Key]
        public int StoopID { get; set; }
        public DateTime stopDt { get; set; }
    }
    public class ViewModel
    {
        public IEnumerable<Teacher> Teachers { get; set; }
        public IEnumerable<Student> Students { get; set; }
        public IEnumerable<Stoopid> Stoopids { get; set; }
    }
}

这是 linq 查询 - 注意 Teacher 没问题,但 Student 和 Stoopid 不行。它们都在同一个 .cs 文件中。我错过了什么吗?

var result = (from t in Teacher
        join s in  Student on t.TeacherId equals s.StudentId
        join st in Stoopid on s.StudentId equals st.StoopID
         where t.TeacherId == 2
         select new
         {
          TeacherID= t.TeacherId,
          Code = t.Code,
           t.Name,
           s.StudentId,
          sCode =s.Code,
          sName=s.Name,
          stopDt= st.stopDt
         })

编辑:我将相关代码添加到 HomeController。我也 运行 通过 LINQPad5 并且它工作正常所以我不知道这是怎么回事 HomeController

您在 Linq 查询中引用了 class 名称,这就是它抛出该错误的原因。您需要引用实际的 List 对象。

var result = (from t in mymodel.Teachers
    join s in mymodel.Students on t.TeacherId equals s.StudentId
    join st in mymodel.Stoopids on s.StudentId equals st.StoopID
     where t.TeacherId == 2
     select new
     {
      TeacherID= t.TeacherId,
      Code = t.Code,
       t.Name,
       s.StudentId,
      sCode =s.Code,
      sName=s.Name,
      stopDt= st.stopDt
     })