Where array of Ids contains array

Jul 21, 2013 at 8:15 PM
Company is object with property int Id.

Is it possibile to write this query like in C# LINQ?

var companyIds = new int[] { 1, 2, 3 };
var companiesFiltered = companies.Where(x => companyIds.Contains(x.Id));
Jul 22, 2013 at 3:12 AM
Edited Jul 22, 2013 at 3:15 AM
This should work for you. Javascript uses IndexOF() instead of contains to find if a value exists inside an array. Hope this helps.
var companyIds = [1,2,3];

var companiesFiltered = Enumerable.from(companies)
.where(function(x) {
                                                     
// If Companies object doesn't contain a match then
// it will return a -1 and the object at the current index will not be added 
// to the list of filtered companies.

 if(companyIds.indexOf(x.Id) > -1){
 return true;  
}else{
 return false;
 } 
                                                        

});

Note: I'm using version 3 so the "where" and "from" are all lower case.


Dec 20, 2013 at 12:03 PM
Thanks you soo much dear danlfarnell