【发布时间】:2012-01-25 07:59:09
【问题描述】:
我使用((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 query expressions can only be constructed from instances that implement the IQueryable interface.
【问题讨论】:
标签: c# linq lambda linq-to-entities expression-trees