【问题标题】:Apply IQueryable filter to parent and child collection in WebApi将 IQueryable 过滤器应用于 WebApi 中的父子集合
【发布时间】:2013-07-01 13:23:51
【问题描述】:

我在 WebApi 请求中使用 IQueryable 功能来提供搜索结果。我的代码基于this article。是否可以不仅在父对象上使用 IQueryable,还可以在子对象的集合上使用?

例如,使用本文中的示例,可以返回对产品的查询。如果 Product 类有一个类别的集合,是否可以对 Product 和它的类别集合执行 IQueryable 操作?像 Products?$filter=Category.Name eq 'Toys' and Price lt 10

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public float Price { get; set; }
    public IEnumerable<Category> Categories { get; set; }
}

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

【问题讨论】:

    标签: c# asp.net-mvc-4 asp.net-web-api iqueryable


    【解决方案1】:

    它应该工作。试试这样的 URL:

    Products?$filter=Categories/any(category: category/Name eq 'Toys') and Price lt 10
    

    这基本上是说“找到至少一个类别名称为‘玩具’且价格低于 10 的所有产品”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-08
      • 1970-01-01
      相关资源
      最近更新 更多