使用 EF 方法的 Linq 无法转换为存储表达式(在存储库模式中)
Linq with EF method cannot be translated into a store expression(in repository pattern)
我正在尝试使用 Ling 子查询从存储库模式中的多个模型访问数据。但是当我尝试使用 .GetQueryable() 访问内部查询中的数据时,我收到以下错误。
错误LINQ to Entities 无法识别方法 'System.Collections.Generic.List1[<>f__AnonymousType170
2[System.Int32,System.Int32]] ToList[<>f__AnonymousType1702](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() 克服此错误或为我提供任何替代方法
Queryable 在运行时被翻译成 sql 查询,在您使用 int 解析的子查询中,c# 编译器知道它不会查询翻译。
用户 IEnumerable 或删除 int as count 将 return int.
问题是在您的 LINQ 查询中使用 ToList 函数,这不是您想要做的,因为它无法转换为正确的 SQL 查询。您只想 在 实际的 LINQ 查询之外使用 ToList。要获取计数 inside,请改用 LINQ Count 函数,例如:
select new
{
StudentSections.ClassID
}).Count()
我正在尝试使用 Ling 子查询从存储库模式中的多个模型访问数据。但是当我尝试使用 .GetQueryable() 访问内部查询中的数据时,我收到以下错误。
错误LINQ to Entities 无法识别方法 'System.Collections.Generic.List1[<>f__AnonymousType170
2[System.Int32,System.Int32]] ToList[<>f__AnonymousType1702](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() 克服此错误或为我提供任何替代方法
Queryable 在运行时被翻译成 sql 查询,在您使用 int 解析的子查询中,c# 编译器知道它不会查询翻译。 用户 IEnumerable 或删除 int as count 将 return int.
问题是在您的 LINQ 查询中使用 ToList 函数,这不是您想要做的,因为它无法转换为正确的 SQL 查询。您只想 在 实际的 LINQ 查询之外使用 ToList。要获取计数 inside,请改用 LINQ Count 函数,例如:
select new
{
StudentSections.ClassID
}).Count()