【发布时间】:2017-09-30 09:23:53
【问题描述】:
在我的 ODATA-v4 控制器中,我有以下代码:
var fn = reportModelBuilder.EntityType<CurrentTestResult>()
.Collection.Function("Breakdown").Returns<int>();
在 CurrentTestResultController.cs 中,我有一个看似简单的:
[EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All)]
[HttpGet]
public IHttpActionResult Breakdown()
{
var count = dataService.GetAll()
.Select(x => x.TestResultTypeId)
.Distinct()
.Count();
return Ok(count);
}
本质上,对于集合中的所有CurrentTestResult 实体,它返回集合中出现的不同TestResultTypeId。 (这是一个微不足道的操作,但我简化了一个更复杂的真实生活场景)
这很容易做到 - 但我似乎无法首先过滤它应该操作的 CurrentTestResult 的集合。
这个请求,默认作用于所有 CurrentTestResult 实体
localhost/app/odatareport/CurrentTestResult/Default.Breakdown
返回
{
@odata.context: "http://localhost/app/odatareport/$metadata#Edm.Int32",
value: 5
}
(正确的结果,有 5 种不同的类型)
但是,这个请求首先尝试简单地过滤它 - 失败
localhost/app/odatareport/CurrentTestResult/Default.Breakdown?$top=2
返回
{
error: {
code: "",
message: "The query specified in the URI is not valid. The requested resource is not a collection. Query options $filter, $orderby, $count, $skip, and $top can be applied only on collections.",
innererror: {
message: "The requested resource is not a collection. Query options $filter, $orderby, $count, $skip, and $top can be applied only on collections.",
type: "Microsoft.OData.ODataException",
stacktrace:
" at System.Web.OData.EnableQueryAttribute.ValidateSelectExpandOnly(ODataQueryOptions queryOptions) at System.Web.OData.EnableQueryAttribute.ExecuteQuery(Object response, HttpRequestMessage request, HttpActionDescriptor actionDescriptor, ODataQueryContext queryContext) at System.Web.OData.EnableQueryAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)"
}
}
}
据我了解 ODATA 管道,为什么会失败是有道理的。控制器方法将返回一个 IQueryable,然后将应用 ODATA $filter、$top 等。
我想要一个函数来操作已经被过滤掉的集合。
我正在尝试做的事情是否可能?
我知道 Breakdown 方法本身有 .GetAll() 在其中,但必须有一种方法可以在方法之前应用过滤 -
否则,这一切都毫无意义....
【问题讨论】:
标签: c# entity-framework odata odata-v4