Alberto
Alberto

Reputation: 199

Pre-filtering OData using an URL param (

I'm using odata with web API on service fabric. I would like to do some per-filtering using a parameter in the URL before to return the IQuerable. Probably that means that I'm mixing the API routing with the odata routing. A not-working example, just to give an idea of what I'm trying to achieve:

[ResponseType(typeof(IQuerable<Something>))]
[HttpGet, Route("Something/{id:guid}/SomeFiltrableList")]
public async Task<IHttpActionResult> GetBySomethingId(Guid id)
{
  var someFiltrableList= await _repository.GetSomething(id);

  return Ok(someFiltrableList.AsQueryable());
}

Would it be possible to do something like this? How? Thanks

Upvotes: 1

Views: 304

Answers (1)

Andriy Tolstoy
Andriy Tolstoy

Reputation: 6090

Add System.Web.OData.Query.ODataQueryOptions parameter and apply it to the data source:

[ResponseType(typeof(IQueryable<Something>))]
[System.Web.Http.HttpGet, System.Web.Http.Route("Something/{id:guid}/SomeFiltrableList")]
public async Task<IQueryable<Something>> Get(Guid id, ODataQueryOptions options)
{
    var someFiltrableList = await _repository.GetSomething(id);

    return options
                .ApplyTo(someFiltrableList)
                .Cast<Something>()
                .AsQueryable();
}

Upvotes: 2

Related Questions