繁体   English   中英

LINQ变量强制转换为AsEnumerator,但是视图抛出错误,表示其为System.Data.Entity.Infrastructure.DbQuery

[英]LINQ variable cast AsEnumerator, yet the view throws error saying its a System.Data.Entity.Infrastructure.DbQuery

我有一个方法:

        var Student = _db.Students.Join(_db.StudentGrades, s => s.ENumber,
           sg => sg.StudentENumber, (s, sg) => new { Student = s, StudentGrade = sg })
           .Where(sc => sc.StudentGrade.LetterGrade == "A").Select(sgl => new SCourse
           {
               LastName = sgl.Student.LastName,
               CourseCode = sgl.StudentGrade.CourseCode,
               CourseNumber = sgl.StudentGrade.CourseNumber
           });
        return View(Student.AsEnumerable());
    }

当我在视图中查看预期结果时,收到一条错误消息,说它以查询而不是可枚举的形式发送到视图。

The model item passed into the dictionary is of 
type 'System.Data.Entity.Infrastructure.DbQuery`1[Practice.ViewModels.SCourse]', 
but this dictionary requires a model item of 
type 'System.Collections.Generic.IEnumerable`1[Practice.ViewModels.AllStudents]'.

为什么会尝试说它正在接收查询项而不是可枚举的项?

如果这可能与视图有关,这是视图的开始:

@model IEnumerable<Practice.ViewModels.AllStudents>

采用

 @model IEnumerable<Practice.ViewModels.SCourse>

代替

 @model IEnumerable<Practice.ViewModels.AllStudents>

您通过了一个名为SCourse的模型。 但是在视图文件中,您需要对AllStudents建模,这就是为什么您会收到此错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM