如何获取 IQueryable.Count 的 ToTraceString
我使用 ((ObjectQuery)IQueryable).ToTraceString()
来获取和调整将由 LINQ 执行的 SQL 代码。
我的问题是,与大多数 IQueryable 方法不同,IQueryable.Count 的定义如下:
public static int Count(this IQueryable source) {
return (int)source.Provider.Execute(
Expression.Call(
typeof(Queryable), "Count",
new Type[] { source.ElementType }, source.Expression));
}
执行查询而不编译并返回 IQueryable。 我想通过这样的方法来实现这一点:
public static IQueryable CountCompile(this IQueryable source) {
return source.Provider.CreateQuery(
Expression.Call(
typeof(Queryable), "Count",
new Type[] { source.ElementType }, source.Expression));
}
但是 CreateQuery 给了我以下异常:
LINQ to Entities 查询表达式只能从实现 IQueryable 接口的实例构造。
I use ((ObjectQuery)IQueryable).ToTraceString()
to obtain and tweak SQL code that is going to be executed by LINQ.
My problem is that unlike most IQueryable methods IQueryable.Count as defined like this:
public static int Count(this IQueryable source) {
return (int)source.Provider.Execute(
Expression.Call(
typeof(Queryable), "Count",
new Type[] { source.ElementType }, source.Expression));
}
executes query without compiling and returning IQueryable.
I wanted to do the trick by something like this:
public static IQueryable CountCompile(this IQueryable source) {
return source.Provider.CreateQuery(
Expression.Call(
typeof(Queryable), "Count",
new Type[] { source.ElementType }, source.Expression));
}
But then CreateQuery gives me the following exception:
LINQ to Entities query expressions can only be constructed from instances that implement the IQueryable interface.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我在尝试这样做时想到的一个实际有效的答案。异常表示“只能从实现 IQueryable 接口的实例构造”,因此答案似乎很简单:返回可查询的内容。返回
.Count()
时可能吗?是的!使用它:
基本上,它的作用是将非 IQueryable 查询包装在子选择中。它将查询转换为
except 让上下文的 QueryProvider 处理查询。生成的 SQL 与您所期望的差不多:
Here's an actual working answer I came up with when I tried to so the same. The exception says "can only be constructed from instances that implement the IQueryable interface", so the answer seems simple: return a queryable something. Is that possible when returning a
.Count()
? Yes!To use it:
Basically, what this does is wrap a non-IQueryable query in a subselect. It transforms the query into
except lets the context's QueryProvider handle the query. The generated SQL is pretty much what you should expect:
您无法为“Count”创建查询对象,因为它不返回 IQueryable(这是有道理的 - 它返回单个值)。
您有两个选择:
(推荐)使用 eSQL:
使用 Reflection 调用不执行 IQueryable 检查的私有方法(由于显而易见的原因,这是错误的,但如果您只是需要它进行调试,则可能会这样)方便):
You cannot create a query object for 'Count' since it is does not return an IQueryable (which makes sense - it returns a single value).
You have two options:
(Recommended) Use eSQL:
Use Reflection to call a private method that doesn't perform the IQueryable check (this is wrong for obvious reasons but if you just need it for debugging, it may be handy):