使用 odata 选择不同的值
我正在尝试创建对 odata webservice 的调用,该调用将仅选择某些属性的不同值。有什么很好的例子说明如何做到这一点吗?
I'm trying to create a call to odata webservice that would select only distinct values of some property. Is there any good example of how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前,OData 协议不支持唯一运算符,或任何其他有助于此类查询的运算符(假设您正在查找某个实体上原始属性的唯一值)。
您应该能够通过在服务器上实现服务操作来解决此问题,该服务操作对通常具有该功能的底层提供程序执行此类查询。然后客户端可以调用该服务操作。
Currently the OData protocol doesn't support the distinct operator, or any other operator which would help with such query (assuming you're looking for disctinct values of a primitive property on some entity).
You should be able to workaround this by implementing a service operation on the server which performs such query on the underlying provider which usually has that ability. And then the client can call that service operation instead.
注意:我知道这已经过时了,但仍然显示在搜索响应中。现在这个问题有一个很好的解决方案。
在 OData v4 中,支持 $apply,除其他外,
$apply
可以用于从结果集中返回一组不同的字段。$apply 是一个需要tl;dr;
基本上,如果我有一个包含许多字段的表,但我只想返回多个特定字段的不同记录,第一步是识别不同的列,我们将它们称为
Name
和 <代码>ID。只需使用
$apply
函数即可获取仅包含 Names 和 Id 列的不同结果集,其语法如下:让我们通过示例执行此操作,
TuberInspections
有 1000 行,但只有少数承包商,我想在下拉列表中显示承包商的名称以在过滤查询中使用。我的不同列是
ContractorName
和ContractorId
如果您的数据结构不够扁平,则对于检索简单的嵌套结果结构的 OOTB 支持有限。
目前,以下语法仅支持单个导航路径。上述查询也可以写为:
嵌套或堆叠的
$apply
转换还有更高级(复杂)的变体,其中包括返回与每个不同结果相对应的唯一行数的计数。有关更多信息,请发布具体问题并使用 OData-v4 进行标记,您将找到所需的所有帮助:)
Note: I know this is old, but still shows up in search responses. There is now a good solution to this problem.
In OData v4, there is support for $apply, amongst other things,
$apply
can be used to return a distinct set of fields from a result set.tl;dr;
Basically, if I have a table that has many fields, but I want to return just the distinct records of a number of specific fields the first step is to identify the distinct columns, lets call them
Name
andId
.Simply use the
$apply
function to get a distinct result set containing just the Names and Id columns with syntax like this:Lets do this by example,
TuberInspections
has 1000s of rows, but only a few contractors, I want to display the names of the contractors in a drop down list to use in a filtering query.My distinct columns are
ContractorName
andContractorId
If your data structure is no flat enough, there is limited OOTB support for retrieving simple nested results structures.
Currently only a single navigation path is supported with the following syntax. The above query can also be written as:
There are more advanced (complicated) variations to nested or stacked
$apply
transformations that include returning a count of the number of unique rows that correspond to each of the distinct results.For more information please post specific questions and tag with OData-v4 and you'll find all the help you need :)