【问题标题】:OData - Nested $filter on collection in $expandOData - 在 $expand 中的集合上嵌套 $filter
【发布时间】:2019-10-08 21:02:55
【问题描述】:

与此示例类似,我想过滤“订单”中的集合。 http://odata.github.io/WebApi/04-03-filter-in-expand/

假设我们有以下模型:

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public IEnumerable<Order> Orders { get; set; }
}

public class Order
{
    public int Id { get; set; }
    public string Name { get; set; }
    public IEnumerable<Article> Articles { get; set; }
}

public class Article
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Category { get; set; }
}

如果我调用以下 URI .../Customers?$expand=Orders($filter=Articles/Category eq 'Cookies'),我会收到一条错误消息

属性“类别”的属性访问的父值不是 单个值。属性访问只能应用于单个值。

但我可以使用如下构造:.../Customers?$expand=Orders($filter=Articles/any(a:a/Category eq 'Cookies'))

anyall 的问题在于,它会根据子级过滤父数据。如果存在带有“类别”的“文章”,它将返回所有文章。但我想减少子项以仅显示“cookie”类别的文章。

【问题讨论】:

    标签: c# asp.net rest wcf odata


    【解决方案1】:

    我认为您需要使用两个嵌套的不同any,如果我理解您,您想过滤它们在Cookies 类别中的所有Articles,我认为您需要类似:

    .../Customers?$filter= Orders/any(o: o/Articles/any(p: p/Category eq 'Cookies'))
    

    如果Orders 有cookie Articles,它将给你所有CustomersOrders

    问候。

    【讨论】:

      猜你喜欢
      • 2017-02-27
      • 2015-07-29
      • 2012-02-28
      • 2015-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-04
      • 2018-08-18
      相关资源
      最近更新 更多