How do you filter on child property collections in Breeze JS?
Asked Answered
P

2

6

I'm trying to filter entities based on a collection of child entities. Here are my entities (EF POCO's):

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 imagined the query to look similar to this:

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

But of course that won't work. Any ideas?

Pardon answered 29/4, 2013 at 12:41 Comment(1)
Breeze now supports this scenario: breezejs.com/documentation/query-examples#Where clauses on related propertiesMariner
A
9

It is not possible with breeze. I recommend you to implement a method in your backed that return all the customers where any Order.Description contains the word 'foo'.

If you are using web API it would be something similar to:

query = entityQuery.from('getCustomerAnyOrderWithFooDescription');

In your backend:

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

Also you can do that more general 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)));
}
Anguiano answered 29/4, 2013 at 12:57 Comment(0)
V
13

As of Breeze 1.4.6, Breeze now supports the "any" and "all" query operators.

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

This means that this query can now be composed as:

var query = breeze.EntityQuery.from("Customers")
  .where("Orders", "any", "Description", "contains", "Foo");
Visayan answered 26/11, 2013 at 19:42 Comment(2)
Whenever I try this syntax, I get the following error: "TypeError: a is undefined"Vignette
Yeah I got it to work fine with an OData backend. This should be the accepted answer now, as the current one is out of date.Intelsat
A
9

It is not possible with breeze. I recommend you to implement a method in your backed that return all the customers where any Order.Description contains the word 'foo'.

If you are using web API it would be something similar to:

query = entityQuery.from('getCustomerAnyOrderWithFooDescription');

In your backend:

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

Also you can do that more general 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)));
}
Anguiano answered 29/4, 2013 at 12:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.