Search code examples
asp.net-core.net-coreasp.net-core-mvcodataasp.net-core-webapi

how to parse ODataQuery /ODataQueryOptions in endpoints that supports OData queries


I'm using net core 3.1., my project contains api controller(ODataController) that supports endpoints that OData queries. I would like to parse OData queries in order to create a custom request to pass some other rest service. How can i create a parser for OData queries ?

some endpoints that supports OData queries.

[HttpGet]
[EnableQuery]
public IEnumerable<WeatherForecast> Get()

[HttpGet]
[ODataRoute]
[EnableQuery(HandleNullPropagation = HandleNullPropagationOption.False, MaxTop = 100, 
AllowedQueryOptions = AllowedQueryOptions.Select |AllowedQueryOptions.Count)]
public IEnumerable<WeatherForecast> Getlist(ODataQueryOptions options)

Solution

  • I suppose you could just break it down into its constituent parts:

    ApplyClause applyClause = options.Apply.ApplyClause;
    bool count = options.Count.Value;
    FilterClause filterClause = options.Filter.FilterClause;
    OrderByClause orderByClause = options.OrderBy.OrderByClause;
    int skip = options.Skip.Value;
    int top = options.Top.Value;
    ⋮
    

    Or to get the string versions:

    string applyClause = options.RawValues.Apply;
    string count = options.RawValues.Count;
    string filterClause = options.RawValues.Filter;
    string orderByClause = options.RawValues.OrderBy;
    string skip = options.RawValues.Skip;
    string top = options.RawValues.Top;
    ⋮