使用 odata 选择不同的值

发布于 2024-09-25 20:49:46 字数 72 浏览 4 评论 0原文

我正在尝试创建对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开柳相依 2024-10-02 20:49:46

目前,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.

美人迟暮 2024-10-02 20:49:46

注意:我知道这已经过时了,但仍然显示在搜索响应中。现在这个问题有一个很好的解决方案。

在 OData v4 中,支持 $apply,除其他外,$apply 可以用于从结果集中返回一组不同的字段。

请参阅相关讨论:将 Distinct 应用于 ODataQuery

来自OData v4 规范
查询选项$apply采用一系列集合转换,用正斜杠分隔以表示它们是连续应用的,例如每个转换的结果是下一个转换的输入
$apply 是一个函数,需要

$apply 是一个需要tl;dr;
基本上,如果我有一个包含许多字段的表,但我只想返回多个特定字段的不同记录,第一步是识别不同的列,我们将它们称为 Name 和 <代码>ID。
只需使用 $apply 函数即可获取仅包含 Names 和 Id 列的不同结果集,其语法如下:

http://url.to/tableresource?$apply=groupby((Name,Id))

让我们通过示例执行此操作,TuberInspections 有 1000 行,但只有少数承包商,我想在下拉列表中显示承包商的名称以在过滤查询中使用。

我的不同列是 ContractorNameContractorId

GET /odata/TuberInspections?$apply=groupby((ContractorName,ContractorId)) HTTP/1.1
Host: localhost:1230

Response:
    { 
        "@odata.context": "http://localhost:1230/odata/$metadata#TuberInspections(ContractorName,ContractorId)",
        "value": [
            {
                "@odata.id": null,
                "ContractorId": 11534,
                "ContractorName": "Kermit d'Frog"
            },
            {
                "@odata.id": null,
                "ContractorId": 11539,
                "ContractorName": "Carlos Spicy Wiener"
            },
            {
                "@odata.id": null,
                "ContractorId": 16827,
                "ContractorName": "Jen Gelfling"
            }
        ]
    }

如果您的数据结构不够扁平,则对于检索简单的嵌套结果结构的 OOTB 支持有限。

如果他的数据结构很难使用,并且您可以控制 API,那么您应该考虑创建一个自定义函数来返回您想要的特定记录集,这是一个微不足道的练习,有许多不同的选项可供您使用,但是超出了本次响应的范围。

<块引用>

此响应针对如何查询现有 OData 服务以获取不同值,而不是如何修改服务以提供此类信息。

目前,以下语法仅支持单个导航路径。上述查询也可以写为:

GET /odata/TuberInspections?$apply=groupby((Contractor/EntityID,Contractor/Initials))
Host: localhost:1230

Response:
    { 
         "@odata.context": "http://localhost:1230/odata/$metadata#TuberInspections(Contractor(EntityID,Initials))",
         "@odata.count": 3,
         "value": [
             {
                 "@odata.id": null,
                 "Contractor": {
                     "@odata.id": null,
                     "Initials": "KdF",
                     "EntityID": 11534
                 }
             },
             {
                 "@odata.id": null,
                 "Contractor": {
                     "@odata.id": null,
                     "Initials": "SW",
                     "EntityID": 11539
                 }
             },
             {
                 "@odata.id": null,
                 "Contractor": {
                     "@odata.id": null,
                     "Initials": "Jen",
                     "EntityID": 16827
                 }
             }
         ]
    } 

嵌套或堆叠的 $apply 转换还有更高级(复杂)的变体,其中包括返回与每个不同结果相对应的唯一行数的计数。

此处记录了组内 $count 不同记录的情况系统查询选项 $apply - groupby 并响应 OData v4 groupby 与 $count

GroupBy 支持 聚合扩展,因此我们可以在前面的 URL 中使用它,记住为投影列名称添加别名

<块引用>

groupby((承包商/实体ID,承包商/缩写),聚合($count as Items))

响应现在包括计数:

<前><代码> {
“@odata.id”:空,
“物品”:260,
“承包商”:{
“@odata.id”:空,
“缩写”:“SW”,
“实体ID”:11539
}
},

有关更多信息,请发布具体问题并使用 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.

See related discussion: Applying Distinct to ODataQuery

from the OData v4 spec,
The query option $apply takes a sequence of set transformations, separated by forward slashes to express that they are consecutively applied, e.g. the result of each transformation is the input to the next transformation
$apply is a function that requires

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 and Id.
Simply use the $apply function to get a distinct result set containing just the Names and Id columns with syntax like this:

http://url.to/tableresource?$apply=groupby((Name,Id))

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 and ContractorId

GET /odata/TuberInspections?$apply=groupby((ContractorName,ContractorId)) HTTP/1.1
Host: localhost:1230

Response:
    { 
        "@odata.context": "http://localhost:1230/odata/$metadata#TuberInspections(ContractorName,ContractorId)",
        "value": [
            {
                "@odata.id": null,
                "ContractorId": 11534,
                "ContractorName": "Kermit d'Frog"
            },
            {
                "@odata.id": null,
                "ContractorId": 11539,
                "ContractorName": "Carlos Spicy Wiener"
            },
            {
                "@odata.id": null,
                "ContractorId": 16827,
                "ContractorName": "Jen Gelfling"
            }
        ]
    }

If your data structure is no flat enough, there is limited OOTB support for retrieving simple nested results structures.

If his data structure is hard to consume and you have control over the API, then you should consider creating a custom Function to return the specific recordset that you desire, that is trivial exercise that has many different options available to you, but outside of the scope of this response.

This response is directed at how to query an existing OData service for distinct values, not how to modify a service to provide such information.

Currently only a single navigation path is supported with the following syntax. The above query can also be written as:

GET /odata/TuberInspections?$apply=groupby((Contractor/EntityID,Contractor/Initials))
Host: localhost:1230

Response:
    { 
         "@odata.context": "http://localhost:1230/odata/$metadata#TuberInspections(Contractor(EntityID,Initials))",
         "@odata.count": 3,
         "value": [
             {
                 "@odata.id": null,
                 "Contractor": {
                     "@odata.id": null,
                     "Initials": "KdF",
                     "EntityID": 11534
                 }
             },
             {
                 "@odata.id": null,
                 "Contractor": {
                     "@odata.id": null,
                     "Initials": "SW",
                     "EntityID": 11539
                 }
             },
             {
                 "@odata.id": null,
                 "Contractor": {
                     "@odata.id": null,
                     "Initials": "Jen",
                     "EntityID": 16827
                 }
             }
         ]
    } 

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.

Including a $count of distinct records within the group is sort of documented here System Query Option $apply - groupby and in response to OData v4 groupby with $count

GroupBy supports an aggregate extension, so we can use this in the previous URL, remember to alias the projected column name.

groupby((Contractor/EntityID,Contractor/Initials),aggregate($count as Items))

the response now includes the count:

       {
            "@odata.id": null,
            "Items": 260,
            "Contractor": {
                "@odata.id": null,
                "Initials": "SW",
                "EntityID": 11539
            }
        },

For more information please post specific questions and tag with OData-v4 and you'll find all the help you need :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文