Question

Is there away to use the "IN" operator with the FilterQueryOp class for Breeze.js? I want to be able to filter by checking if an ID is in an array. In Mysql it would be a simple

SELECT * FROM a_table WHERE fieldname IN ('array1', 'array2', 'array3')

But I can't find a simple way to do this with breeze?

Was it helpful?

Solution

There is no actual 'IN' operator but you can easily accomplish the same thing by 'or'ing together several breeze predicates and passing the composite predicate to the EntityQuery.where method. So if you start with

var Predicate = breeze.Predicate;
var p1 = Predicate.create('fieldName', '==', 'array1');
var p2 = Predicate.create('fieldName', '==', 'array2');
var p3 = Predicate.create('fieldName', '==', 'array3');

Then given these 3 predicates you 'or' them together like so:

var newPred = Predicate.or(p1, p2, p3);

or

var newPred = p1.or(p2, p3);

or

var preds = [p2, p3];
var newPred = p1.or(preds);

or via a more fluent approach:

var newPred = Predicate.create("fieldName", "==", "array1")
                            or("fieldName", "==", "array2")
                           .or("fieldName", "==", "array3")    

Obviously, you can also create your own javascript function to perform this composition for you as well.

In any event, once you have the composite predicate, you can then write something like:

var myQuery = breeze.EntityQuery.from("a_table").where(newPred);

OTHER TIPS

Looks like there is an IN filterQueryOp available now:

http://breeze.github.io/doc-js/api-docs/classes/FilterQueryOp.html#property_In

I presume this implements Jay's original workaround by OR'ing together a set of predicates? In that case what is the practical limit to the number of criteria that can used with IN before the girth of the server-side OR breaks down?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top