检测何处应用于IQueryable< T>
如何检测 IQueryable< T>
是否有应用哪些
过滤器?
How can I detect if a IQueryable<T>
has a where
filter applied?
在这段代码中,我需要以编程方式知道 queryFiltered
有一个其中
In this code, I need to know programmatically that queryFiltered
has a where
applied to it and query
doesn't
IQueryable<Customer> query = Context.Customers;
IQueryable<Customer> queryFiltered = Context.Customers
.Where(c=>c.Name.Contains("ABC"));
您将不得不解析 表达式
这是从 返回的 属性 $ c> IQueryable< T>
实现。
You will have to parse the Expression
that is returned from the Expression
property on the IQueryable<T>
implementation.
您必须查询 Queryable.Where
方法在您爬行表达式
树时被调用。
You'll have to query for the Queryable.Where
method being called as you crawl the Expression
tree.
另请注意,虽然可查询。
将是最常见的检测$ c> c 使用
指令);如果您有某些不使用 Queryable.Where
扩展方法的内容,那么您必须明确地查找(或使用更通用的过滤方法来其中
方法采用 IQueryable< T>
并返回一个 IQueryable< T>
)。
Also note that while Queryable.Where
is going to be the most common way to detect a where
filter, query syntax allows for other implementations to be used (depending on what namespaces are used in the using
directives); if you have something that is not using the Queryable.Where
extension method then you'll have to look for that explicitly (or use a more generic method of filtering for a Where
method that takes an IQueryable<T>
and returns an IQueryable<T>
).
ExpressionVisitor
class (as Expression 树,我强烈建议您使用方法作为处理您的表达式
树的基础。
The ExpressionVisitor
class (as pointed out by xanatos) provides a very easy way of crawling the Expression
tree, I highly recommend using that approach as a base for processing your Expression
tree.
值得注意的是 ExpressionVisitor
类实现需要在类级别上存储和暴露状态。因此,最好(IMO)创建一个执行该操作的内部类,然后再创建一个公共方法,它创建一个新的 ExpressionVisitor
的实例时间;这将有助于处理突变状态,如果正确完成,将允许方法线程安全(如果这是您的关注)。
Of note is that ExpressionVisitor
class implementations are required to store and expose state on the class level. Because of that, it would be best (IMO) to create internal classes that perform the action one-time and then have a public method which creates a new instance of the ExpressionVisitor
every time; this will help with dealing with mutating state, and if done properly, will allow the method to be thread-safe as well (if that is a concern of yours).