6

我正在尝试根据子实体的集合过滤实体。这是我的实体(EF POCO):

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

public class Order
{
    public int Id { get; set; }
    public string Description { get; set; }
}

使用 Breeze js,我想返回任何 Order.Description 包含单词“foo”的所有客户。我想象查询看起来与此类似:

query = entityQuery.from('Customers')
                   .where("Orders.Description", "contains", "foo");

但这当然行不通。有任何想法吗?

4

2 回答 2

13

从 Breeze 1.4.6 开始,Breeze 现在支持“any”和“all”查询运算符。

请参阅:http ://www.breezejs.com/documentation/query-examples

这意味着这个查询现在可以组成:

var query = breeze.EntityQuery.from("Customers")
  .where("Orders", "any", "Description", "contains", "Foo");
于 2013-11-26T19:42:49.960 回答
9

微风是不可能的。我建议您在您的支持中实现一个方法,该方法返回任何 Order.Description 包含单词“foo”的所有客户。

如果您使用的是 Web API,它将类似于:

query = entityQuery.from('getCustomerAnyOrderWithFooDescription');

在您的后端:

[HttpGet]
public IQueryable<Customer> getCustomerAnyOrderWithFooDescription()
{
  return _contextProvider.Context.Customers.Where(c.Orders.Any(o => o.Description.Contains('foo')));
}

你也可以做这样的更一般的事情:

query = entityQuery.from('getCustomerAnyOrderWithDescription').withParameters('foo');

[HttpGet]
public IQueryable<Customer> getCustomerAnyOrderWithDescription([FromBody] String someText)
{
  return _contextProvider.Context.Customers
      .Where(c.Orders.Any(o => o.Description.Contains(someText)));
}
于 2013-04-29T12:57:43.577 回答