【问题标题】:LINQ to Entities does not recognize the method 'Boolean CheckMeetingSettings(Int64, Int64)' methodLINQ to Entities 无法识别“Boolean CheckMeetingSettings(Int64, Int64)”方法
【发布时间】:2012-04-05 07:58:21
【问题描述】:

我正在使用 EDM 中的代码优先方法并面临一个我无法解决的错误。请帮助我

LINQ to Entities 无法识别方法 'Boolean CheckMeetingSettings(Int64, Int64)' 方法,该方法不能 翻译成商店表达式。

我的代码如下(这是我写的查询

from per in obj.tempPersonConferenceDbSet
           where per.Conference.Id == 2
           select new PersonDetials
           {
               Id = per.Person.Id,
               JobTitle = per.Person.JobTitle,
               CanSendMeetingRequest = CheckMeetingSettings(6327,per.Person.Id)
           }

public bool CheckMeetingSettings(int,int)
{
  ///code I have written.
}

请帮帮我。

【问题讨论】:

    标签: linq entity-framework c#-4.0 linq-to-entities


    【解决方案1】:

    EF 无法将自定义代码转换为 SQL。尝试迭代结果集并在 LINQ 查询之外分配属性。

    var people = (from per in obj.tempPersonConferenceDbSet
               where per.Conference.Id == 2
               order by /**/
               select new PersonDetials
               {
                   Id = per.Person.Id,
                   JobTitle = per.Person.JobTitle,
               }).Skip(/*records count to skip*/)
                 .Take(/*records count to retrieve*/)
                 .ToList();
    
    people.ForEach(p => p.CanSendMeetingRequest = CheckMeetingSettings(6327, p.Id));
    

    【讨论】:

    • 感谢您的回复,但我也需要分页。在我的数据库中,我有超过 1 个 lac 用户。请提供分页...
    • @user1314655 使用SkipTake 方法进行分页。您还需要使用order by 对查询进行排序。请参阅我编辑的答案。
    【解决方案2】:

    使用 Entity Framework,您不能将在数据库服务器上运行的代码与在应用程序内部运行的代码混合在一起。编写这样的查询的唯一方法是在 SQL Server 中定义一个函数来实现您编写的代码。

    有关如何将该函数公开给 LINQ to Entities 的更多信息,请参见here

    或者,您必须在初始查询之外调用 CheckMeetingSettings,如 Eranga 所示。

    【讨论】:

    • 感谢您的建议,我会遵循它
    【解决方案3】:

    试试:

    var personDetails = obj.tempPersonConferenceDbSet.Where(p=>p.ConferenceId == 2).AsEnumerable().Select(p=> new PersonDetials
               {
                   Id = per.Person.Id,
                   JobTitle = per.Person.JobTitle,
                   CanSendMeetingRequest = CheckMeetingSettings(6327,per.Person.Id)
               });
    
    public bool CheckMeetingSettings(int,int)
    {
      ///code I have written.
    }
    

    您必须使用AsEnumerable(),这样您才能执行CheckMeetingSettings

    【讨论】:

    • 感谢您的回复。当我使用此代码时,我遇到了一些其他错误,即{“已经有一个打开的 DataReader 与此命令关联,必须先关闭。”}跨度>
    【解决方案4】:

    Linq to Entities 无法将您的自定义代码转换为 SQL 查询。

    您可以考虑首先只选择数据库列,然后添加一个 .ToList() 来强制解析查询。获得这些结果后,您可以再次选择从 CheckMeetingSettings 方法中添加信息的位置。

    我更喜欢流畅的语法,所以我在下面的例子中使用了它。

    var query = obj.tempPersonConferenceDbSet
                   .Where(per => per.Conference.Id == 2).Select(per => new { Id = per.Person.Id, JobTitle = per.Person.JobTitle })
                   .ToList()
                   .Select(per => new PersonDetails { Id = per.Id, 
                                                      JobTitle = per.JobTitle,
                                                      CanSendMeetingRequest = CheckMeetingSettings(6327, per.Person.Id) })
    

    如果您的 CheckMeetingSettings 方法还访问数据库,您可能要考虑不使用单独的方法来防止 SELECT N+1 场景,并尝试以数据库可以理解的方式将逻辑表达为查询的一部分。

    【讨论】:

    • 感谢它正在工作,但执行查询需要花费大量时间。我们能否有另一种方法来快速完成它。请帮助我
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多