简体   繁体   English

是否可以在调用Where的地方调用named方法?

[英]Is it possible to call named method within a call to Where?

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 我试图从RedGate的这本免费电子书中了解Linq的一些性能影响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. 在本书的第157-158页,他们创建了以下示例。

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. 然后他们将lamda表达式的一部分重新考虑到以下函数中。

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. 基于我到目前为止所研究的内容,它不可能运行这个linq查询,因为LINQ将无法识别该方法并且无法转换为存储表达式。 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. 你是对的,LINQ-to-Entities无法按照它的显示方式调用它。

The only way it could be used in LINQ-to-Entities is to: 它可以在LINQ-to-Entities中使用的唯一方法是:

As casperOne said, you can't do that with a function. 正如casperOne所说,你不能用一个函数做到这一点。 This answer is a way to do what you need to do (reuse a where filter). 这个答案是一种做你需要做的事情的方法(重用where过滤器)。

You can create an Expression somewhere you can access, then reuse it. 您可以在可以访问的位置创建Expression ,然后重复使用它。

System.Linq.Expressions.Expression<Func<Account, bool>> pastDueAccountFunc = account => account.DueDate < System.Data.Objects.EntityFunctions.AddDays(DateTime.Now, -7);

Then to use it later: 然后再使用它:

var pastDueAccounts = context.Accounts.Where(pastDueAccountFunc);

This is the full code I'm using with this: 这是我正在使用的完整代码:

System.Linq.Expressions.Expression<Func<SomeNeatEntity, bool>> func = x => x.DateCreated < System.Data.Objects.EntityFunctions.AddDays(DateTime.Now, -7);

var entities = new MyEntities();

var t = entities.SomeNeatEntities.Where(func);
Console.WriteLine(t.Count());

var h = entities.SomeNeatEntities.Where(x => x.SomeField != null).Where(func);
Console.WriteLine(h.Count());

MyEntities is the ObjectContext in the project. MyEntities是项目中的ObjectContext
entities.SomeNeatEntities is ObjectSet<SomeNeatEntity> . entities.SomeNeatEntitiesObjectSet<SomeNeatEntity>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM