带EF方法的Linq不能转换为存储表达式(在存储库模式中)

本文关键字:存储 表达式 模式 方法 EF Linq 转换 不能 | 更新日期: 2023-09-27 18:02:51

我正在尝试使用Ling子查询从存储库模式中的多个模型访问数据。但是当我试图访问数据在内部查询使用. getqueryable ()我收到以下错误:

ERRORLINQ to Entities不识别方法'System. collections . generic . list 1[<>f__AnonymousType170 2[System. int32,System. int32]Int32]] ToList[<>f__AnonymousType170 2](System.Collections.Generic.IEnumerable 1[<>f__AnonymousType170 ' 2[System.Int32,System.Int32]])'方法,该方法不能转换为存储表达式。

var query = (from i in
                                 (from student in _tblStudents.GetQueryable()
                                  join Sections in _tblStudentsSections.GetQueryable() on student.StudentID equals Sections.StudentID
                                  join Class in _tblClasses.GetQueryable() on Sections.ClassID equals Class.ClassID
                                  join Program in _tblPrograms.GetQueryable() on Class.ProgramID equals Program.ProgramID
                                  //where student.IsForeign == false
                                  select new
                                  {
                                      ProgramID = Program.ProgramID,
                                      program = Program.ProgramName,
                                      ClassIDs = Sections.ClassID,
                                      TotalSeats = Program.NoOfAdmissions,
                                      IsForeign = student.IsForeign
                                  })
                             group i by new { i.ProgramID, i.IsForeign, i.TotalSeats, i.program } into grp
                             select new AdmissionSummaryReportModel
                             {
                                 program = grp.Key.program,
                                 TotalSeats = grp.Key.TotalSeats,
                                 //SeatsFilled = grp.Select(m => m.ClassIDs).Count(),
                                 AvailableForeignSeats = 22,
                                 SeatsFilled = (int)(from student in _tblStudents.GetQueryable()
                                                            join StudentSections in _tblStudentsSections.GetQueryable() on student.StudentID equals StudentSections.StudentID
                                                            join Class in _tblClasses.GetQueryable() on StudentSections.ClassID equals Class.ClassID
                                                            join Program in _tblPrograms.GetQueryable() on Class.ProgramID equals Program.ProgramID
                                                     where student.IsForeign == false && Program.ProgramID == grp.Key.ProgramID
                                                            select new
                                                            {
                                                                StudentSections.ClassID
                                                            }).ToList().Count(),
                                 ForeignSeatsFilled = (int)(from student in _tblStudents.GetQueryable()
                                                            join StudentSections in _tblStudentsSections.GetQueryable() on student.StudentID equals StudentSections.StudentID
                                                            join Class in _tblClasses.GetQueryable() on StudentSections.ClassID equals Class.ClassID
                                                            join Program in _tblPrograms.GetQueryable() on Class.ProgramID equals Program.ProgramID
                                                            where student.IsForeign && Program.ProgramID == grp.Key.ProgramID
                                                            select new
                                                            {
                                                                StudentSections.ClassID
                                                            }).ToList().Count()
                             }).ToList();

如何克服这个错误与。getqueryable()或提供给我任何替代方法

带EF方法的Linq不能转换为存储表达式(在存储库模式中)

问题是在LINQ查询中使用ToList函数,这不是您想要做的,因为它不能转换为适当的SQL查询。您希望在实际的LINQ查询之外仅使用ToList 。要获取内部的计数,请使用LINQ count函数,例如:

select new                                                       
{
StudentSections.ClassID
}).Count()

Queryable在运行时被翻译成sql查询,在子查询中,你正在用int进行解析,这是c#编译器不知道查询翻译的。user IEnumerable或remove int as count都将返回int。