I have a working (simplified) ODataController with the following method.
public class MyTypeController : ODataController
{
  [HttpGet]
  [EnableQuery]
  [ODataRoute("myTypes")]
  public IQueryable<MyType> GetMyTypes(ODataQueryOptions<MyType> options)
  {
    return _repo.myResultsAsQueryable();
  }
}
I would like to be able to call this method from the server and to do this I need to instantiate an ODataQueryOptions which requires an ODataQueryContext.
There are examples of how to do this (Eg. here and here) but they all seem to reference a previous version of OData. The ODataQueryContext constructor currently requires a third argument (ODataPath path) which is not addressed in any examples that I can find.
Edit: @snow_FFFFFF, Here's some more context... I realize that I can simply consume the OData endpoint via a HttpClient but I would like to interact with the IQueryable directly as you say.
The problem is that the application I'm working on allows users to create filters (like a sophisticated search engine) that can be saved and later recalled by other users. From a JS client, they simply lookup the filter by id, and issue a query against the OData endpoint with the filter applied to the query string. This works very well from the client-side but I would like to be able to do something similar from the server-side as well.
This is what I would like to do but how can I instantiate the ODataPath argument?
public IQueryable<MyType> FilterMyTypes(int filterID)
{
  // lookup filter by filterID from db...
  filter = "$filter=Status eq 1"; // for example...
  ODataPath path = // but how can I get the path!!!
  new ODataQueryContext(edmModel, typeof(MyType), path); 
  var uri = new HttpRequestMessage(HttpMethod.Get, "http://localhost:56339/mytypes?" + filter);
  var opts = new ODataQueryOptions<MyType>(ctx, uri);
  var results = new MyTypeController().GetMyTypes(opts);
}
Another application of this would be to support dynamic grouping as below:
[HttpGet]
[Route("myTypes/{filterID:int}/groupby/{groupByFieldName}")]
public IHttpActionResult GroupMyTypes(int filterID, string groupByFieldName)
{
  // For example: get all Active MyTypes and group by AssignedToUserID...
  // Get the results of the filter as IQueryable...
  var results = FilterMyTypes(filterID);
  // group on groupByFieldName
  var grouped = results.GroupBy(x => GetPropertyValue(x,groupByFieldName));
  // select the groupByFieldName and the count
  var transformedResults = grouped.Select(g => new { g.Key, Count = g.Count() });
  return Ok(transformedResults);
}
                Sure. ODataPath is a list of ODataPathSegment(s) which should follow up the OData Uri spec.
In Web API OData, it's easy to instantiate an ODataPath, for example:
IEdmModel model = GetEdmModel(); 
IEdmEntitySet entitySet = model.EntityContainer.FindEntitySet(setName); 
ODataPath path = new ODataPath(new EntitySetPathSegment(entitySet)); 
The above path follows up the OData spec that it has the odata template as:
~/entityset
More test cases (codes) can be found here
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