没有被应用ODataQueryOptions [英] ODataQueryOptions not being applied

查看:231
本文介绍了没有被应用ODataQueryOptions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能失去了一些东西简单,但在此基础上的博客文章:<一href=\"http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options\" rel=\"nofollow\">http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options这应该是工作。我有以下控制器方法:

I may be missing something simple but based on this blog post: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options this should be working. I have the following controller method:

public virtual IQueryable<DtoAgent> Get(ODataQueryOptions<Agent> options, bool includeInactive = false, bool includeDeleted = false)
    {
        IQueryable<Agent> agents = null;
        if (includeDeleted && includeInactive)
        {
            agents = agentRepository.FindAll();
        }
        else if (includeDeleted)
        {
            agents = agentRepository.FindBy(a => a.ussiStatus == "A");
        }
        else if (includeInactive)
        {
            agents = agentRepository.FindBy(a => !a.IsDeleted);
        }
        if (agents == null)
        {
            agents = agentRepository.FindByExp(a => a.ussiStatus == "A" && !a.IsDeleted);
        }
        options.ApplyTo(agents);
        return agents.ToDtos<DtoAgent>();
    }

当我这样称呼它../api/Agent?$top=10它返回所有的结果不只是10.我可以看到变量的选项TopQueryOption,但它似乎并没有得到应用。它的工作原理,如果我使用[可查询]属性,但DB调用这正是我想避免后顶被应用。我在全球范围内调用EnableQuerySupport并同时拥有的NuGet包并安装更新2012.2。感谢您的帮助。

when I call it like ../api/Agent?$top=10 it returns all results not just 10. I can see the TopQueryOption in the options variable but it does not appear to get applied. It works if I use the [Queryable] attribute but the top is applied after the DB call which is what I am trying to avoid. I am calling EnableQuerySupport at the global level and have both the Nuget package and 2012.2 update installed. Thanks for your help.

推荐答案

您失去了一些东西简单。当你调用ApplyTo,它不会发生变异的IQueryable的,它返回应用查询。所以,这样的事情应该工作,而不是:

You are missing something simple. When you call ApplyTo, it doesn't mutate the IQueryable, it returns the applied query. So something like this should work instead:

var queryResults = options.ApplyTo(agents) as IQueryable<Agent>;
return queryResults.ToDtos<DtoAgent>();

这篇关于没有被应用ODataQueryOptions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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