在自定义IQueryable double上应用WebAPI OData $ Skip [英] WebAPI OData $Skip on custom IQueryable double applied

查看:129
本文介绍了在自定义IQueryable double上应用WebAPI OData $ Skip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了通过WebAPI OData端点公开的自定义IQueryable.控制器的Get()的结构相当标准:

I have implemented a custom IQueryable that is exposed via a WebAPI OData endpoint. The structure of the controller's Get() is rather standard:

[EnableQuery(
    AllowedQueryOptions = AllowedQueryOptions.Count
                          | AllowedQueryOptions.Filter
                          | AllowedQueryOptions.OrderBy
                          | AllowedQueryOptions.Skip
                          | AllowedQueryOptions.Top)]
[ODataRoute]
public PageResult<Foo> Get(ODataQueryOptions<Foo> queryOptions)
{

    var bars = new QueryableData<Foo>(_provider);

    var result = ((IQueryable<Foo>)queryOptions
        .ApplyTo(bars,
            new ODataQuerySettings(new ODataQuerySettings { EnableConstantParameterization = false, EnsureStableOrdering = false }))).ToList();
    var count = _provider.Count;
    return new PageResult<Foo>(result, null, count);
}

我看到的奇怪行为是,在返回PageResult后,在查询字符串中应用了OData $ Skip.例如:

The odd behavior I am seeing, is that an OData $Skip in the query string is applied after the PageResult is returned. For example:

  • 如果查询字符串包含?$ top = 10& $ skip = 10,则不会返回任何结果.
  • 如果查询字符串包含?& top = 12& skip = 10,将返回(2)个结果.

我想做的是防止框架将跳过"应用到我的结果集中,因为查询提供程序已经在实现跳过.是否可以设置ODataQuerySettings来防止这种跳过的双重应用?

What I am looking to do is prevent the framework(s) from applying the Skip to my results set since the query provider is already implementing the skip. Are there ODataQuerySettings that can be set to prevent this double application of the skip?

经过进一步调查,当我从查询字符串中删除$ count = true时,按预期跳过(和顶部)函数.这使我相信我实现$ count = true的方法是错误的.从我的调试会话中可以看出,当$ count = true在查询选项中时,可查询对象对其两次应用了表达式树,一次使用返回类型为long的返回树,然后再次使用不包含换行的countlong表达式.我尝试在第一次通过时返回计数,然后在第二次通过时可正确查询,但是这会导致跳过表达式的延迟应用.在这里似乎缺少一些非常基本的东西.

Upon further investigation, when I remove $count=true from the query string skip (and top) function as expected. This leads me to believe that my approach to implementing $count=true is incorrect. From my debugging sessions it appears that when $count=true is in the query options the queryable has the expression tree applied to it twice, once with a return type of long, and then again without the wrapping countlong expression. I have tried returning the count on the first pass and then proper queryable for the second pass, but this results in the delayed application of the skip expression. There seems be be something very fundamental that I am missing here.

推荐答案

在阅读Github问题列表时,我遇到了这篇文章:

While reading through the Github issues list I came across this post: OData PageResult method ignoring count parameter when using EnableQuery attribute #159. What appears to be the problem is the combination of EnableQuery Attribute and the parameterized Get constructor taking the ODataQueryOptions. Using both means that you will implement the constructor query options, applying the query expressions, then the framework will apply what filters it can on direction from the applied attribute; therefore double applying things like skip, top and orderby.

这篇关于在自定义IQueryable double上应用WebAPI OData $ Skip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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