【问题标题】:Linq with EF method cannot be translated into a store expression(in repository pattern)带有 EF 方法的 Linq 无法转换为存储表达式(在存储库模式中)
【发布时间】:2015-10-04 07:01:22
【问题描述】:

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

错误LINQ to Entities 无法识别方法'System.Collections.Generic.List1[<>f__AnonymousType1702[System.Int32,System.Int32]] ToList[f__AnonymousType1702](System.Collections.Generic.IEnumerable1 [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() 克服此错误或为我提供任何替代方法

【问题讨论】:

    标签: c# .net linq entity-framework repository-pattern


    【解决方案1】:

    问题在于在您的 LINQ 查询中使用 ToList 函数,这不是您想要做的,因为它无法转换为正确的 SQL 查询。您只想实际的 LINQ 查询之外使用 ToList。要获得 inside 的计数,请改用 LINQ Count 函数,例如:

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

    【讨论】:

    • LINQ to Entities 无法识别方法 'System.Linq.IQueryable`1[sCMS.Dal.EntityModels.STD_Students] GetQueryable()' 方法,并且此方法无法转换为存储表达式。现在得到这个错误。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2017-11-04
      • 1970-01-01
      • 2011-04-20
      • 1970-01-01
      • 1970-01-01
      • 2018-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多