How do you filter collections of child objects in Breeze JS?

I am trying to filter objects based on a collection of child objects. Here are my entities (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; }
}

Using Breeze js I want to return all customers where any Order.Description contains the word "foo". I assumed that the query looks something like this:

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

But of course this will not work. Any ideas?

+5
source share
2 answers

This is not possible with breezes. I recommend that you implement a method in your support that returns all customers where any Order.Description contains the word "foo".

If you use the web API, it will be something similar to:

query = entityQuery.from('getCustomerAnyOrderWithFooDescription');

In its backend:

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

You can also do this more generally by doing something like this:

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)));
}
+8
source

Breeze 1.4.6, Breeze "" "" .

: http://www.breezejs.com/documentation/query-examples

, :

var query = breeze.EntityQuery.from("Customers")
  .where("Orders", "any", "Description", "contains", "Foo");
+12

All Articles