OData $filter 包含 $expand 中的项目 [英] OData $filter with items in a $expand

查看:62
本文介绍了OData $filter 包含 $expand 中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提供了一些网络服务来访问信息.

I have given some web services to access informations.

我尝试扩展节点的第一件事.我已经使用以下代码成功完成了

The first thing that i have tries to expand a node . And i have done that successfully with following code

http://www.domain.com/ODataService/WorkService.svc/CaseStudies?format=json&$expand=ServiceOfferings

现在我想过滤扩展 ServiceOfferings 时将获得的 ServiceOfferingID.如何对扩展集合使用过滤器选项

Now i want to filter ServiceOfferingID that i will get when expanding ServiceOfferings . How can use filter option against a expanded collection

http://www.domain.com/ODataService/WorkService.svc/CaseStudies?format=json&$expand=ServiceOfferings&$filter=ServiceOfferings.ServiceOfferingID eq 127 

但它不起作用.这样做的正确方法是什么

But its not working. What is right way to do the same

推荐答案

您需要编写的查询取决于 扩展集合的基数.

The query you'll need to write depends on the cardinality of the expanded collection.

以下是一些使用公共 示例 OData Northwind 服务(由 odata.org 提供)的示例.

Here are some examples that use the public sample OData Northwind service, provided by odata.org.

一个订单总是由一个客户完成.

查找具有特定名称的客户下的订单:http://services.odata.org/V3/Northwind/Northwind.svc/Orders?$expand=Customer&$filter=Customer/CompanyName eq 'Vins et alcools Chevalier'.这相当于Dhawal的回答.

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.

一个客户可以发出多个订单.

使用量词 allany 指定您是否希望至少一个命令或所有命令遵守您的条件.

Use the quantifiers all or any to specify whether you want at least one, or all of the orders to obey your conditions.

  1. 查找已由特定员工处理的一个或多个订单的客户:http://services.odata.org/V3/Northwind/Northwind.svc/Customers?$expand=Orders&$filter=Orders/any(o: o/EmployeeID等式 9)
  2. 查找长时间未订购任何商品的客户:http://services.odata.org/V3/Northwind/Northwind.svc/Customers?$expand=Orders&$filter=Orders/all(o: o/OrderDate lt DateTime'1997-01-01')
  1. Find customers for which one or more orders have been processed by a specific employee: http://services.odata.org/V3/Northwind/Northwind.svc/Customers?$expand=Orders&$filter=Orders/any(o: o/EmployeeID eq 9)
  2. Find customers that haven't ordered anything for a long time: http://services.odata.org/V3/Northwind/Northwind.svc/Customers?$expand=Orders&$filter=Orders/all(o: o/OrderDate lt DateTime'1997-01-01')

您可以调用http://services.odata.org/V3/Northwind/Northwind.svc/$metadata 并检查 NavigationProperty 元素,以查看存在哪些关系.

You can call http://services.odata.org/V3/Northwind/Northwind.svc/$metadata and inspect the NavigationProperty elements, to see which relations exist.

<NavigationProperty Name="Orders" 
    Relationship="NorthwindModel.FK_Orders_Customers" 
    ToRole="Orders" 
    FromRole="Customers"/>

然后,查找与该名称的关联,您将找到基数:

Then, look for an association with that name and you'll find the cardinality:

<Association Name="FK_Orders_Customers">
    <End 
         Type="NorthwindModel.Customer" 
         Role="Customers" 
         Multiplicity="0..1"/>
    <End 
         Type="NorthwindModel.Order" 
         Role="Orders" 
         Multiplicity="*"/>
    ...

导航这样的一对多关系:http://services.odata.org/V3/Northwind/Northwind.svc/Customers?$expand=Orders&$filter=Orders/EmployeeID eq 9,将给你:属性‘EmployeeID’的属性访问的父值不是单个值.属性访问只能应用于单个值."

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."

导航与所有或任何的多对一关系,例如 http://services.odata.org/V3/Northwind/Northwind.svc/Orders?$expand=Customer&$filter=Customer/any(c: c/CompanyName eq 'Vins et alcools Chevalier'),会给你:任何/全部只能在集合后使用."

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."

顺便说一下,all()any() 实际上是 通用量词、∀() 和存在量词、分别是∃(),你可能在数学课上还记得.

By the way, all() and any() are actually the Universal quantifier, ∀() and the existential quantifier, ∃(), respectively, which you may remember from math class.

这篇关于OData $filter 包含 $expand 中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆