1

簡単なものが欠けているかもしれませんが、このブログ投稿に基づいています: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-optionsこれはすべきです働く。次のコントローラーメソッドがあります。

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 だけでなくすべての結果が返されます。options 変数に TopQueryOption が表示されますが、適用されていないようです。[Queryable] 属性を使用すると機能しますが、回避しようとしている DB 呼び出しの後にトップが適用されます。グローバル レベルで EnableQuerySupport を呼び出しており、Nuget パッケージと 2012.2 更新プログラムの両方がインストールされています。ご協力いただきありがとうございます。

4

1 に答える 1

4

シンプルなものが欠けています。ApplyTo を呼び出すと、IQueryable は変更されず、適用されたクエリが返されます。したがって、代わりに次のようなものが機能するはずです。

var queryResults = options.ApplyTo(agents) as IQueryable<Agent>;
return queryResults.ToDtos<DtoAgent>();
于 2013-04-18T16:17:59.807 に答える