oData WCF数据服务筛选器子集合 [英] oData WCF Data Services Filter Child Collection

查看:77
本文介绍了oData WCF数据服务筛选器子集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列客户,每个客户都有一组订单.

I have an array of customers, each customer has a collection of orders.

以下查询返回客户及其订单并按预期工作:

the query below returns customers and their orders and works as expected:

~Customers?$expand=Orders

现在,我想吸引现有客户,但按Order.Amount> 100筛选他们的订单(我很高兴没有此类订单的客户保留在列表中),

Now I want to get the existing customers but filter their orders by Order.Amount > 100 (I'm happy for the customers having no such orders to remain in the list),

当我尝试以下操作时:

~Customers?$expand=Orders&$filter=Orders/Amount gt 100

我遇到以下错误:

The 'Amount' is not allowed at position ***. Member access or specifying a type identifier on a resource set reference is not allowed.

我可以遍历客户并致电

~Customers('Blah')/Orders?$filter=Amount gt 100 

这是可行的,但我真的很想一口气完成它.

which works, but I'd really like to get it done in one go.

您能建议我如何做到这一点吗?

Could you advice on how do I accomplish this?

推荐答案

这里有两点会引起问题.首先,听起来您想过滤Orders集合,而不是Customers集合(即,您始终希望返回所有Customer,但只希望扩展某些Orders).当您拥有一个看起来像EntitySet?$filter=<some predicate>的URI时,过滤器将始终应用于EntitySet,在您的情况下为Customers(不是您想要的).

There are two things causing problems here. First, it sounds like you want to filter the Orders collection, not the Customers collection (i.e., you always want all Customers to be returned, but you only want certain Orders to be expanded). When you have a URI that looks like EntitySet?$filter=<some predicate>, the filter always applies to the EntitySet, which in your case is Customers (which is not what you want).

第二,~Customers?$expand=Orders&$filter=Orders/Amount gt 100甚至没有正确过滤客户的原因是因为Orders是实体的集合,而不是单个实体.如果将订单视为列表,则Orders/Amount没有任何意义,只有Order/Amount可以.如果您希望有一个过滤器仅返回具有至少一个Order/Amount大于100的订单的客户,则可以使用any关键字:

Secondly, the reason ~Customers?$expand=Orders&$filter=Orders/Amount gt 100 doesn't even filter the Customers correctly is because Orders is a collection of entities, not a single entity. Orders/Amount doesn't make sense if you think of Orders as a list, only Order/Amount does. If you wanted to have a filter that only returns Customers who have at least one Order where Order/Amount is greater than 100, you could use the any keyword:

~Customers?$expand=Orders&$filter=Orders/any(o: o/Amount ge 100)

但是,这再次过滤了客户,而不是订单.

But again, that filters the Customers, not the Orders.

鉴于您实际上想过滤订单,以下内容对您有用吗?

Given that you want to actually be filtering the Orders, would the following work for you?

~Orders?$expand=Customer&$filter=Amount gt 100

这篇关于oData WCF数据服务筛选器子集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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