检测何处应用于IQueryable< T> [英] Detect where applied to IQueryable<T>

查看:111
本文介绍了检测何处应用于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的表达式属性 $ 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 过滤器的方法,查询语法允许其他imp要使用的索引(取决于 使用指令);如果您有某些不使用 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).

这篇关于检测何处应用于IQueryable&lt; T&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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