Search code examples
breeze

BreezeJS "WHERE value IN array"


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?


Solution

  • 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.