如何实例化ODataQueryOptions [英] How to Instantiate ODataQueryOptions

查看:207
本文介绍了如何实例化ODataQueryOptions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下方法工作(简化)了ODataController.

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();
  }
}

我希望能够从服务器调用此方法,为此,我需要实例化需要ODataQueryContextODataQueryOptions.

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.

这里有一些操作方法的示例(例如,此处 ODataQueryContext 构造函数当前需要第三个参数(ODataPath路径),在我可以找到的任何示例中都没有解决.

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.

@snow_FFFFFF,这里还有更多上下文...我意识到我可以简单地通过HttpClient消耗OData端点,但是我想像您所说的那样直接与IQueryable进行交互.

@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.

问题是我正在使用的应用程序允许用户创建过滤器(例如复杂的搜索引擎),该过滤器可以保存并随后由其他用户调用.从JS客户端,他们只需按id查找过滤器,然后针对OData终结点发出查询,并将过滤器应用于查询字符串.从客户端来看,这很好用,但是我也希望能够从服务器端做类似的事情.

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.

这是我想做的,但是如何实例化ODataPath参数呢?

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);
}

推荐答案

好的. ODataPath是ODataPathSegment的列表,应该跟进

Sure. ODataPath is a list of ODataPathSegment(s) which should follow up the OData Uri spec.

在Web API OData中,实例化ODataPath很容易,例如:

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)); 

上面的path遵循了OData规范,它具有如下的odata模板:

The above path follows up the OData spec that it has the odata template as:

~/entityset

可以找到更多的测试用例(代码)

More test cases (codes) can be found here

这篇关于如何实例化ODataQueryOptions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆