ScottyMacDev
ScottyMacDev

Reputation: 182

How to use predicates on expanded entities in breeze

Lets say that I have entity called Foo and it has a list of Bars. Bars also contains a field called isDeleted which indicates that the particular bar has been soft deleted. I want to be able to query for a specific Foo and filter out the Bars that are soft deleted inside of a Javascript file using Breeze. I thought that this would work...

var query = breeze.EntityQuery.from('Foo')
        .where('Id', '==', id).and('Bars.IsDeleted', '==', false)
        .expand('Bars');

However it does not, can someone tell me how to do this in breeze? If the only solution is to code a method in the BreezeController and use standard LINQ syntax then I am fine with that but I just want to see if it will work at all in Breeze first.

Upvotes: 0

Views: 377

Answers (2)

Jay Traband
Jay Traband

Reputation: 17052

Right now Breeze does not support filtering of 'expands'. i.e. only returning the 'expanded' entities that meet a certain criteria. This is because very few back end servers and persistence libraries provide a mechanism to support this.

What you may be able to do however is invert the query to get what you need. i.e.

var query = breeze.EntityQuery.from('Bars').expand('Foo')
    .where(new breeze.Predicate('Foo.Id', 'eq', id)
                 .and('IsDeleted', 'eq', false));

This assumes that 'Bar' entities each have a scalar 'Foo' property.

This will return just the 'Bars' that you want each with its associated 'Foo' entity. This works because Breeze allows nested scalar predicates. i.e. 'Foo.Id'.

Upvotes: 1

Makarray
Makarray

Reputation: 71

Multiple .where() chains on a query will automatically "AND" together. If you want to do this explicity, you need to use the breeze.Predicate object as your where parameter like so:

var query = breeze.EntityQuery.from('Foo').expand('Bars')
    .where(new breeze.Predicate('Id', 'eq', id)
                     .and('Bars.IsDeleted', 'eq', false));

You can also choose to use .or or any other method on the Predicate object, most of them chain properly.

Edit: Here is an example of using multiple .where calls:

    var query = breeze.EntityQuery.from('Foo').expand('Bars')
                .where('Id', 'eq', id)
                .where('Bars.IsDeleted', 'eq', false);

Upvotes: 1

Related Questions