是否可以在对Where 的调用中调用命名方法?

2024-05-10

我试图从 RedGate 的这本免费电子书中了解 Linq 的一些性能影响ftp://support.red-gate.com/ebooks/under-the-hood-of-net-memory-management-part1.pdf ftp://support.red-gate.com/ebooks/under-the-hood-of-net-memory-management-part1.pdf

在本书的第 157-158 页,他们创建了以下示例。

Order[] pastDueAccounts = null;  
DateTimedueDate = DateTime.Today.AddDays(-7);  
using(varcontext = new Context())    
{  
    pastDueAccounts = context.Accounts.Where(account => account.DueDate < dueDate).ToArray();  
} 

然后他们将 lambda 表达式的一部分重构为以下函数。

public bool PastDueAccount(Account account)  
{  
    return account.DueDate < DateTime.Today.AddDays(-7);  
} 

最后他们如下使用这个函数。

Order[] pastDueAccounts = null;  
using(varcontext = new Context())  
{  
    pastDueAccounts = context.Accounts.Where(account => PastDueAccount(account)).ToArray();  
} 

根据我迄今为止的研究,不可能运行此 linq 查询,因为 LINQ 将无法识别该方法并且无法转换为存储表达式。我想知道这个例子是否是错误的并且根本无法运行,或者我是否只是很难了解如何模拟这个问题?


您是对的,这将无法通过 LINQ-to-Entities 的显示方式进行调用。

在 LINQ-to-Entities 中使用它的唯一方法是:

  • 在服务器端创建与该功能等效的功能。
  • 在模型中映射函数 https://stackoverflow.com/a/7001201/50776适当地以便它正确地翻译呼叫。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

是否可以在对Where 的调用中调用命名方法? 的相关文章

随机推荐