我想从OData端点获取不同值的列表.但不支持distinct或group by.
我的URI查询看起来像这样
GET /odata/Products?$select=foo & $top=10 & $count=true & distinct=true
Run Code Online (Sandbox Code Playgroud)
我的控制器
[EnableQuery]
public IQueryable<FooBarBaz> Get(ODataQueryOptions<FooBarBaz> queryOptions, bool distinct)
{
//I've tried the following
return Repository.AsQueryable().Distinct();
// and
return Repository.AsQueryable().GroupBy(x => x.Foo);
// and
IQueryable query = queryOptions.ApplyTo(Repository.AsQueryable());
return query.Distinct(); // Can't call .Distinct() here
}
Run Code Online (Sandbox Code Playgroud)
没有工作:(
我将 OData v4 与 WebAPI 与这些 nuget 包一起使用:
Microsoft.AspNet.WebApi.OData 4.0.30506
我无法groupby与计数一起工作。这应该是最简单的事情。
我有一个简单的表/实体,名为Delivery. 它有一个状态列(在其他几个列中)。我基本上想做的是这个 SQL 查询,但是使用 OData:
SELECT e.status, count(*) AS total
FROM Delivery e
GROUP BY e.status
Run Code Online (Sandbox Code Playgroud)
我试过使用countdistinct,但它没有给我正确的结果。
/odata/deliveries?$apply=groupby((status),aggregate(status with countdistinct as total))
Run Code Online (Sandbox Code Playgroud)
它返回:
"value": [
{
"@odata.id": null,
"total": 1,
"status": 1
},
{
"@odata.id": null,
"status": 2,
"total": 1
},
{
"@odata.id": null,
"status": 4,
"total": 1
}
]
Run Code Online (Sandbox Code Playgroud)
正确的结果应该是(这是我的 SQL 查询返回的):
status total
1 2
2 22
4 1 …Run Code Online (Sandbox Code Playgroud)