OData $filter,其中包含 $expand 中的项目

发布于 2025-01-03 12:07:12 字数 487 浏览 1 评论 0 原文

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

我要做的第一件事就是尝试扩展一个节点。我已经使用以下代码成功完成了此操作

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 

但它不起作用。做同样的事情的正确方法是什么

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

美人骨 2025-01-10 12:07:12

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

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

订单始终由一位客户完成。

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

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

使用量词 全部任何指定您是否希望至少一个或所有订单遵守您的条件。

  1. 查找已由特定员工处理一个或多个订单的客户:
    http://services.odata.org/V3/Northwind/Northwind.svc/Customers?$expand=Orders&$filter=Orders/任意(o: o/EmployeeID eq 9)
  2. 查找很长时间没有订购任何商品的客户:
    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 元素,查看存在哪些关系。

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

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

<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”的属性访问的父值不是单个值。属性访问只能应用于单个值。”

导航与所有或任何,例如 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.

  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')

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="*"/>
    ...

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() and any() are actually the Universal quantifier, ∀() and the existential quantifier, ∃(), respectively, which you may remember from math class.

眼趣 2025-01-10 12:07:12

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'

你又不是我 2025-01-10 12:07:12

可能对某人有帮助

GET serviceRoot/People?$expand=Trips($filter=Name eq 'Trip in US')

Might be helpful for someone

GET serviceRoot/People?$expand=Trips($filter=Name eq 'Trip in US')
梦冥 2025-01-10 12:07:12

在 OData 中,“过滤器”命令仅适用于顶级元素。为了使过滤器正常工作,您需要具有以下 URL

http://www.example.com/ODataService/WorkService.svc/CaseStudies(x)/ServiceOfferings?format=json&$filter=ServiceOfferingID eq 127< /code>

显然这不是您要编写的查询,但在幕后您的查询将被转换为表达式树,该表达式树具有基于顶级元素的根表达式。

如果您确实需要过滤数据,您可能会拦截查询并编写自己的表达式,如下所示:

[QueryInterceptor("CaseStudies")]
public Expression<Func<CaseStudie, bool>> CaseStudieFilter()
{
    <Expression here>
}

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:

[QueryInterceptor("CaseStudies")]
public Expression<Func<CaseStudie, bool>> CaseStudieFilter()
{
    <Expression here>
}
江心雾 2025-01-10 12:07:12

您还可以通过服务上的 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.

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