简体   繁体   English

如何将过滤器选项从ODataQueryOptions映射到RestRequest

[英]How to map filter options from ODataQueryOptions to RestRequest

I need to be able to convert from ODataQueryOptions to RestRequest in order to be able to issue a RestRequest with specified filters, and have created the following helper class: 我需要能够从ODataQueryOptions转换为RestRequest ,以便能够使用指定的过滤器发出RestRequest,并创建了以下帮助程序类:

public static class ODataQueryFilterToRestClient
{
    public static RestRequest Map(ODataQueryOptions odataQuery)
    {
        var restRequest = new RestRequest();

        if (odataQuery.Filter != null)
        {
            restRequest.AddQueryParameter(@"$filter", odataQuery.Filter.RawValue);
        }

        if (odataQuery.Top != null)
        {
            restRequest.AddQueryParameter(@"$top", odataQuery.Top.RawValue);
        }

        if (odataQuery.Skip != null)
        {
            restRequest.AddQueryParameter(@"$skip", odataQuery.Skip.RawValue);
        }

        if (odataQuery.OrderBy != null)
        {
            restRequest.AddQueryParameter(@"$orderby", odataQuery.OrderBy.RawValue);
        }
        //etc
        return restRequest;
    }
}

Given that OdataQueryOptions supports the following: 鉴于OdataQueryOptions支持以下内容:

在此输入图像描述

Is there a simpler way to make the conversion between ODataQueryOptions to RestClient, or another rest client proxy? 有没有更简单的方法在ODataQueryOptions与RestClient或其他其他客户端代理之间进行转换?

On a side note, I don't know if there's a better way to accept parameters through a controller than ODataQueryOptions? 另外,我不知道是否有更好的方法通过控制器接受参数而不是ODataQueryOptions?

There is no direct support of ODataQueryOptions in RestSharp . 没有直接支持的ODataQueryOptionsRestSharp

There are other clients designed specifically for querying with OData , eg Simple.OData.Client . 还有其他专门用于查询OData客户端,例如Simple.OData.Client However it also doesn't operate with ODataQueryOptions for requests, providing fluent API. 但是,对于请求,它也不能与ODataQueryOptions一起运行,从而提供流畅的API。

Overall ODataQueryOptions is rather used on a server-side in OData compatible RESTful APIs. 总体而言, ODataQueryOptionsOData兼容的RESTful API中用于服务器端。 Clients (including RestSharp ) just use their regular syntax to provide the data for request. 客户端(包括RestSharp )只使用其常规语法来提供请求数据。

So answering your question (Is there a simpler way...) - No, there is not. 所以回答你的问题(有更简单的方法......) - 不,没有。

However your conversion method looks nice and pretty simple. 但是,您的转换方法看起来很简单。 If I had to make a call with RestSharp for given ODataQueryOptions , I'd do this exactly in the same way. 如果我必须使用RestSharp与给定的ODataQueryOptions进行调用,我ODataQueryOptions完全相同的方式执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM