I managed to do it as follows:
ODataPath path = Request.GetODataPath(); IEdmType edmType = path.EdmType; private ODataQueryOptions GetODataQueryOptions(IEdmType edmType) { IEdmModel model = Models.ModelBuilder.GetEdmModel(); ODataQueryContext queryContext = new ODataQueryContext(model, edmType); ODataQueryOptions queryOptions = new ODataQueryOptions(queryContext, this.Request); return queryOptions; }
This works for most query parameters, but with $ select and $ expand errors: type 'Collection ([Org.Microsoft.Product Nullable = False])' is not an entity type. Only object types support $ select and $ expand. Is there a way to avoid this exception gracefully when the client is trying to filter with $ select and $ expand, or should I just write something like
if (Request.RequestUri.Query.Contains("select")) { return errormessage }
Also, more importantly, how to apply these query parameters to the EdmEntityObjectCollection, which is returned in the first method?
queryOptions.ApplyTo(collectionProduct.AsQueryable());
(it might be better to use the dynamic assembly of your collection regarding query parameters)
source share