简体   繁体   English

如何将 OData $orderby 与函数一起使用

[英]How to use OData $orderby with functions

I am querying data from backend server by Rest API using OData v3.0.我正在使用 OData v3.0 通过 Rest API 从后端服务器查询数据。

I have a table (and the associated resource URL) Contact and each contact has either CompanyName or PersonName .我有一个表(和相关的资源 URL) Contact ,每个联系人都有CompanyNamePersonName How ever in frontend there's only a Name displayed.但是在前端只显示一个Name

What I want to achieve is to $orderby these two fields whichever is available.我想要实现的是$orderby这两个可用的字段。 A possible way is the concatenation of these two fields, for example $orderby=concat(CompanyName,PersonName) , but I got this error一种可能的方法是这两个字段的连接,例如$orderby=concat(CompanyName,PersonName) ,但我收到了这个错误

Please check your OData query: Only ordering by properties at the root level is supported for non-primitive collections.请检查您的 OData 查询:非原始 collections 仅支持根级别的属性排序。 Nested properties and expressions are not supported.不支持嵌套属性和表达式。

Is there any other way I can do this orderby?有没有其他方法可以做到这一点? Thanks.谢谢。

The question问题

How can I order by EITHER one field or the other?我如何按一个字段或另一个字段排序?

The hypothetical answer假设的答案

You can use an expression in your orderby, as you did, here is a working example:您可以像以前一样在 orderby 中使用表达式,这是一个工作示例:

https://services.odata.org/V3/Northwind/Northwind.svc/Customers/?$orderby=concat(Phone,Fax) https://services.odata.org/V3/Northwind/Northwind.svc/Customers/?$orderby=concat(电话,传真)

But why does it not work?但为什么它不起作用?

According to the ODATA specification, this should work, but the truth is, that most backend implementations do not full comply to the specification.根据 ODATA 规范,这应该可行,但事实是,大多数后端实现并不完全符合规范。 That is a pretty normal thing.这是很正常的事情。

Many backend implementations I know just do not SUPPORT nested properties or expressions in its orderby.我知道的许多后端实现都不支持其 orderby 中的嵌套属性或表达式。

Especially when the backend is provided by systems that base on relational databases (older SAP Systems, NAV or AX Systems..) they tend to not support it, mostly because they want to directly push the query down to the database, or they just didnt implement it up to this day.特别是当后端由基于关系数据库的系统(旧的 SAP 系统、NAV 或 AX 系统)提供时,他们往往不支持它,主要是因为他们想直接将查询推送到数据库,或者他们只是不支持实施至今。

You never can assume that the backend completely implements the whole ODATA specs, most of the time, the backend implements only what was needed so far.您永远不能假设后端完全实现了整个 ODATA 规范,大多数时候,后端只实现了目前为止所需要的。

So, the practical answer所以,实际的答案

This is not an ODATA specification problem.这不是 ODATA 规范问题。 Your approach is correct.你的方法是正确的。 Instead, you have a not fully implemented backend.相反,您有一个未完全实现的后端。 Your only solution is to ask them to implement it, or handle it on the client side.您唯一的解决方案是要求他们实现它,或者在客户端处理它。

By the way顺便一提

ODATA v3 is a very strange and rarely supported version of ODATA. ODATA v3 是一个非常奇怪且很少支持的 ODATA 版本。 Normally, there is ODataV2 or ODataV4 around nowadays.通常,现在有 ODataV2 或 ODataV4。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM