LINQ查询的重用 [英] Reuse of a LINQ query
问题描述
这是不是一个结果的重用,但更多的语句本身。
也不是全盘错误使用VAR时,作为中提到:的LINQ to SQL:重用lambda表达式
This is not about the reuse of a result but more the statement itself. Nor is it about an error when using var as mentioned in: LINQ to SQL: Reuse lambda expression
出于纯粹的好奇,我想知道是否有可能重新使用一个单一的LINQ语句。
Out of sheer curiosity I was wondering if it is possible to reuse a single LINQ statement.
可以说我有以下LINQ语句:
Lets say I have the following LINQ statement:
.Where(x => x.Contains(""));
是否可以提取声明 X => x.Contains()
和使用某种参考这个在以后的使用,可以说,另一个类?
Is it possible to extract the statement x => x.Contains("")
and use some kind of reference to this for later usage in, lets say, another class?
所以我可以这样调用它:。凡(previouslySavedStatement);
So I can call it like: .Where(previouslySavedStatement);
推荐答案
您可以将它存储在变量中。如果您正在使用的IQueryable
工作,然后使用:
You can store it in a variable. If you are working with IQueryable
then use:
System.Linq.Expressions.Expression<Func<Foo, bool>> selector = x => x.Contains("");
如果您使用的是的IEnumerable
然后使用:
If you are using IEnumerable
then use:
Func<Foo, bool> selector = x => x.Contains("");
和您的查询中使用它:
query.Where(selector);
这篇关于LINQ查询的重用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!