OData $filter,其中包含 $expand 中的项目
我提供了一些网络服务来访问信息。
我要做的第一件事就是尝试扩展一个节点。我已经使用以下代码成功完成了此操作
http://www.domain.com/ODataService/WorkService.svc/CaseStudies?format=json&$expand=ServiceOfferings
现在我想过滤扩展 ServiceOfferings 时将获得的 ServiceOfferingID 。 如何对扩展集合使用过滤选项
http://www.domain.com/ODataService/WorkService.svc/CaseStudies?format=json&$expand=ServiceOfferings&$filter=ServiceOfferings.ServiceOfferingID eq 127
但它不起作用。做同样的事情的正确方法是什么
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要编写的查询取决于扩展集合的基数。
以下是一些使用公共示例 OData Northwind 服务(由 odata.org 提供)的示例 。
订单始终由一位客户完成。
查找具有特定名称的客户所下的订单:
http://services.odata.org/V3/Northwind/Northwind.svc/Orders?$expand=Customer&$filter=Customer/CompanyName eq 'Vins et alcools Chevalier'。这相当于Dhawal的回答。
一个客户可以发出多个订单。
使用量词 全部或任何指定您是否希望至少一个或所有订单遵守您的条件。
http://services.odata.org/V3/Northwind/Northwind.svc/Customers?$expand=Orders&$filter=Orders/任意(o: o/EmployeeID eq 9)
http://services.odata.org/V3/Northwind/Northwind.svc/Customers?$expand=Orders&$filter=Orders/全部(o: o/OrderDate lt DateTime'1997-01-01')
您可以调用http://services.odata.org/V3/Northwind/Northwind.svc/$metadata 并检查 NavigationProperty 元素,查看存在哪些关系。
然后,查找与该名称的关联,您将找到基数:
导航一对多关系,如下所示: http://services.odata.org/V3/Northwind/Northwind.svc/Customers?$expand=Orders&$filter=Orders/EmployeeID eq 9,将为您提供:“属性“EmployeeID”的属性访问的父值不是单个值。属性访问只能应用于单个值。”
导航与所有或任何,例如 http://services.odata.org/V3/Northwind/Northwind.svc/Orders?$expand=Customer&$filter=Customer/any(c: c/CompanyName eq 'Vins et alcools Chevalier' ),将为您提供:
“任何/全部只能在收集后使用。”
顺便说一句,
all()
和any()
实际上是 通用量词、∀() 和存在量词,分别是 ∃(),您可能在数学课上还记得。The query you'll need to write depends on the cardinality of the expanded collection.
Here are some examples that use the public sample OData Northwind service, provided by odata.org.
An order is always done by exactly one customer.
Find the orders made by a customer with a specific name:
http://services.odata.org/V3/Northwind/Northwind.svc/Orders?$expand=Customer&$filter=Customer/CompanyName eq 'Vins et alcools Chevalier'. This is equivalent to the answer of Dhawal.
A customer can issue many orders.
Use the quantifiers all or any to specify whether you want at least one, or all of the orders to obey your conditions.
http://services.odata.org/V3/Northwind/Northwind.svc/Customers?$expand=Orders&$filter=Orders/any(o: o/EmployeeID eq 9)
http://services.odata.org/V3/Northwind/Northwind.svc/Customers?$expand=Orders&$filter=Orders/all(o: o/OrderDate lt DateTime'1997-01-01')
You can call http://services.odata.org/V3/Northwind/Northwind.svc/$metadata and inspect the NavigationProperty elements, to see which relations exist.
Then, look for an association with that name and you'll find the cardinality:
Navigating a one-to-many relationship like this: http://services.odata.org/V3/Northwind/Northwind.svc/Customers?$expand=Orders&$filter=Orders/EmployeeID eq 9, will give you: "The parent value for a property access of a property 'EmployeeID' is not a single value. Property access can only be applied to a single value."
Navigating a many-to-one relationship with all or any, like http://services.odata.org/V3/Northwind/Northwind.svc/Orders?$expand=Customer&$filter=Customer/any(c: c/CompanyName eq 'Vins et alcools Chevalier'), will give you:
"Any/All may only be used following a collection."
By the way,
all()
andany()
are actually the Universal quantifier, ∀() and the existential quantifier, ∃(), respectively, which you may remember from math class.oData 支持按子对象的属性进行过滤。
这是一个例子:
http://services.odata .org/Northwind/Northwind.svc/Orders?$filter=客户/国家/地区 eq '德国'
Filtering by child object's properties is supported in oData.
Here is an example:
http://services.odata.org/Northwind/Northwind.svc/Orders?$filter=Customer/Country eq 'Germany'
可能对某人有帮助
Might be helpful for someone
在 OData 中,“过滤器”命令仅适用于顶级元素。为了使过滤器正常工作,您需要具有以下 URL
http://www.example.com/ODataService/WorkService.svc/CaseStudies(x)/ServiceOfferings?format=json&$filter=ServiceOfferingID eq 127< /code>
显然这不是您要编写的查询,但在幕后您的查询将被转换为表达式树,该表达式树具有基于顶级元素的根表达式。
如果您确实需要过滤数据,您可能会拦截查询并编写自己的表达式,如下所示:
In OData the Filter command only works on the top level element. For your filter to work you would need to have the following URL
http://www.example.com/ODataService/WorkService.svc/CaseStudies(x)/ServiceOfferings?format=json&$filter=ServiceOfferingID eq 127
Obviously this isn't the query you are trying to write, but behind the scenes your query is being converted to an expression tree which has a root expression based on the top level element.
If you really required to filter the data you could potentially intercept the query and write your own expression as below:
您还可以通过服务上的 webget 来完成此操作。我不得不做一些类似于按属性的属性进行过滤的事情。
You can also accomplish this through a webget on the service. I have had to do something similar to filter by properties of properties.