实现 IQueryable.Count

发布于 2024-10-04 18:15:10 字数 685 浏览 2 评论 0原文

我正在开发一个 IQueryable 提供程序。在我的 IQueryProvider 中,我有以下代码:

public TResult Execute<TResult>(Expression expression)
{
    var query = GetQueryText(expression);

    // Call the Web service and get the results.
    var items = myWebService.Select<TResult>(query);

    IQueryable<TResult> queryableItems = items.AsQueryable<TResult>();
    return (TResult)queryableItems;
}

GetQueryText 完成所有的工作并计算出表达式树的查询字符串。这一切都运行良好,因此Where、OrderBy 和Take 已排序。 Web 服务支持使用以下内容进行计数查询:

int count = myWebService.Count(query);

但我无法理解将其放入 IQueryable 或 IQueryProvider 中的位置。

我基本上是通过阅读教程和开源示例来工作的,但似乎找不到一个可以计算的。

I'm working on an IQueryable provider. In my IQueryProvider I have the following code:

public TResult Execute<TResult>(Expression expression)
{
    var query = GetQueryText(expression);

    // Call the Web service and get the results.
    var items = myWebService.Select<TResult>(query);

    IQueryable<TResult> queryableItems = items.AsQueryable<TResult>();
    return (TResult)queryableItems;
}

GetQueryText does all the leg work and works out the query string for the expression tree. This is all working well, so Where, OrderBy and Take are sorted. The webservice supports a count query using the following:

int count = myWebService.Count(query);

But I can't get my head round where I put this in the IQueryable or IQueryProvider.

I've basically worked from reading tutorials and open source examples, but can't seem to find one that does Count.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦年海沫深 2024-10-11 18:15:10

答案似乎比我最初想象的要简单。 这篇博文帮助:

Execute 方法是提供程序实际执行查询表达式的入口点。显式执行而不是仅仅依赖 IEnumerable.GetEnumerator() 很重要,因为它允许执行不一定产生序列的表达式。例如,查询“myquery.Count()”返回单个整数。此查询的表达式树是对返回整数的 Count 方法的方法调用。 Queryable.Count 方法(以及其他聚合等)使用此方法“立即”执行查询。

我做了一些调试,对于 myContext.Where(x => x.var1 > 5) 的查询,调用了 Execute 并且 TResult 是一个 IEnumerableMyClass>

对于 myContext.Where(x => x.var1 > 5).Count() 执行被调用并且 TResult 是一个 int

所以我的 Execute 方法只需要正确返回即可。

The answer appears simpler than I first thought. This blog post helped:

The Execute method is the entry point into your provider for actually executing query expressions. Having an explicit execute instead of just relying on IEnumerable.GetEnumerator() is important because it allows execution of expressions that do not necessarily yield sequences. For example, the query “myquery.Count()” returns a single integer. The expression tree for this query is a method call to the Count method that returns the integer. The Queryable.Count method (as well as the other aggregates and the like) use this method to execute the query ‘right now’.

I did some debugging and for a query of myContext.Where(x => x.var1 > 5) Execute is called and TResult is an IEnumerable<MyClass>

For myContext.Where(x => x.var1 > 5).Count() Execute is called and TResult is an int

So my Execute method just needs to return appropriately.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文