Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BreezeJS "WHERE value IN array"

Tags:

breeze

Can specify in the where clause that I want the data that has the column value equal to some values from an array?

For example:

EntityQuery.from('Customers')
.where('DepartmentID','in','[3,5,6]');

Or how else should I do it efficiently since the table has a lot of entries an it wouldn't be efficient to retrieve all of them? Is it efficient if I do it one by one?

like image 698
Razvan Avatar asked Sep 20 '13 13:09

Razvan


3 Answers

Using Breeze's new JSON query feature introduced in 1.5.1, you can create a “WHERE value IN array” clause like this:

var jsonQuery = {
    from: 'Customers',
    where: {
        'DepartmentID': { in: [3,5,6] }
    }
}
var query = new EntityQuery(jsonQuery);
like image 157
Gavin.Paolucci.Kleinow Avatar answered Jan 02 '23 01:01

Gavin.Paolucci.Kleinow


Just add multiple predicates -

var myArray = [3, 4, 5];
var predicate = new Breeze.Predicate;

var query = EntityQuery.from('Customers');

if (myArray) {
    var criteriaPredicate = null;
    $.each(myArray, function (index, item) {
        criteriaPredicate = (index === 0)
            ? Predicate.create('DepartmentId', '==', item)
            : criteriaPredicate.or('DepartmentId', '==', item);
        if (Predicate.isPredicate(criteriaPredicate)) {
            predicate = predicate.or(criteriaPredicate);
        }
    });
}

query = query.where(predicate);

That may not run 100% correctly but should show you what to do - create predicates dynamically and add them to a total predicate and then to the query.

like image 31
PW Kad Avatar answered Jan 02 '23 02:01

PW Kad


A bit late to the party but I needed the same thing as was able to do it like this:

public getFeaturedRestaurants(restaurantIds: number[]) { this.dataBreezeService.initialiseQuery('getFeaturedRestaurants', [restaurantIds]);

let query = new EntityQuery()
    .from('restaurants')
    .where("restaurantId", "in", restaurantIds)
    .toType('Restaurant')
    .expand('foodType, suburb.city')

return this.dataBreezeService.executeQueryCacheOrServerForList(query, false);

}

like image 36
Rodney Avatar answered Jan 02 '23 01:01

Rodney