可以在调用Where中调用named方法吗? [英] Is it possible to call named method within a call to Where?

查看:190
本文介绍了可以在调用Where中调用named方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从RedGate
ftp://support.red-gate.com/ebooks/under-the-hood-of-net-memory-management-part1.pdf



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

  Order [] pastDueAccounts = null;日期时间
using(varcontext = new Context())
{
pastDueAccounts = context.Accounts.Where(account => account.DueDate< dueDate).ToArray();
}

然后,他们将lamda表达式的一部分重新定义为以下函数。 >

  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到实体不能以显示的方式调用。



在LINQ到实体中使用的是:




I am trying to understand some performance implication of Linq from this free ebook by RedGate ftp://support.red-gate.com/ebooks/under-the-hood-of-net-memory-management-part1.pdf

On page 157-158 in this book, they created following example.

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

They then re-factored part of lamda expression into following function.

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

Finally they used this function as follows.

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

Based on what I researched so far, its not possible to run this linq query as LINQ will not be able recognize the method and cannot be translate into a store expression. I wonder if this example is wrong and simply not possible to run or if I am just having hard time getting my heard around on how to simulate this problem?

解决方案

You are correct, this would not be able to be called by LINQ-to-Entities the way it's displayed.

The only way it could be used in LINQ-to-Entities is to:

  • Create the equivalent of the function on the server side.
  • Map the function in the model appropriately so that it translates the call correctly.

这篇关于可以在调用Where中调用named方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆