Reputation: 144
Here is my issue. I am using ASP.NET Web API 2.0 and the QueryableAttribute to take advantage of some of the OData filtering functionality.
public class VideoController : ApiController
{
[HttpGet]
[Route("activevideos")]
[Queryable]
public IEnumerable<Video> GetActiveVideos(ODataQueryOptions<Video> options)
{
return new GetvContext().Videos.Where(c => c.IsActive);
}
}
Now, I have a class that I have been using to modify the response object and contained entities. This was working fine before I started using the QueryableAttribute. Before this, I was returning a List from the previous method instead of IEnumerable.
public class TestMessageProcessHandler : MessageProcessingHandler
{
protected override HttpResponseMessage ProcessResponse(
HttpResponseMessage response, CancellationToken cancellationToken)
{
var content = ((ObjectContent)(response.Content)).Value;
// Do something here with the content. This used to be a List<Video>
// but now the object it is of type:
// System.Data.Entity.Infrastructure.DbQuery<System.Web.Http.OData.Query.Expressions.SelectExpandBinder.SelectSome<Content.Api.Video>>
}
}
I need to be able to get the entity from this and I am not sure how to get from type:
System.Data.Entity.Infrastructure.DbQuery<System.Web.Http.OData.Query.Expressions.SelectExpandBinder.SelectSome<Content.Api.Video>>
to something like List<Video>
so I can modify the Video
object.
Upvotes: 5
Views: 4409
Reputation: 1891
Most probably you are getting this problem because you specified which fields should be selected by your OData query (for example, $select=Nr,Date
). If you won't do this, after applying query options OData won't return a SelectSome
objects, it will return the same objects you are querying, in your case Video
. So you can easily modify these objects server-side before returning them to client.
And if you need to exclude properties from Video
object which you don't need, instead of $select
query option you can use a DTO instead of your Video
object and include in this DTO only those fields you need.
Yes, this is a workaround, and hopefully there will be better means to do deal with server-side logic in the future versions of this library
Upvotes: 0
Reputation: 7467
Can't you just do a ToList() - the DbQuery implements IQueryable...
e.g.
var content = ((ObjectContent)(response.Content)).Value;
var queryable = content as IQueryable<Video>;
var list = queryable.ToList();
Upvotes: 0
Reputation: 14580
Remove the [Queryable]
attribute and manage the querying of the data yourself - something like this:
public class VideoController : ApiController
{
[HttpGet]
[Route("activevideos")]
public IList<Video> GetActiveVideos(ODataQueryOptions<Video> options)
{
var s = new ODataQuerySettings() { PageSize = 1 };
var result = options.ApplyTo(
new GetvContext().Videos.Where(c => c.IsActive), s)
.ToList();
return result;
}
}
Upvotes: 1