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?
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With