Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify odata results after query

I have an ASP.NET Web API app using Entity Framework and Odata.

I would like to modify the results of a query when using a GET... currently, within the controller, you effectively just pass the EntityFramework data entities back to the Odata handlers...

[EnableQuery]
public IQueryable<myEntity> GetLineItem()
{
    return db.myEntities;
}

It's simple to prepend whatever query Odata passes into this by simply returning a subset

return db.myEntity.Where(myEntity => myEntity.Name == "Bob")

Odata will add whatever is in the $filter querystring parameter to the expression passed in here and you get the subset of these results.

However, I would like to iterate over the results once the query executes and the SQL results are parsed into entity objects.

I have tried creating a wrapper that implements the IQueryable interface and hooking into the GetEnumerator methods and the same for the IProvider and hooking into the execute method. Odata doesn't seem to be using either one of these.

Is there a way to do this?

like image 323
Blaize Stewart Avatar asked Apr 29 '15 19:04

Blaize Stewart


1 Answers

You don't have to return IQuerable from controller when working with OData. Check "Invoking Query Options Directly" section at https://docs.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options For your case it will looks like:

public HttpResponseMessage Get(ODataQueryOptions<myEntity> opts)
{
    var settings = new ODataValidationSettings();

    opts.Validate(settings);

    var intermediateResult = opts.ApplyTo(db.myEntities).ToArray();

    var result = //change intermediateResult as you wish.

    return result;
}
like image 188
Ihar Yakimush Avatar answered Oct 06 '22 14:10

Ihar Yakimush