About contains

Nov 12, 2013 at 12:27 AM
Contains multiple conditions is how to achieve ?
Nov 12, 2013 at 6:47 AM
The contains function doesn't really deal with conditions--it deals more with asking whether the collection has, as an element, the item passed to the contains function (with an optional "comparer" function to define the sense that a given element of the collection is "equal" to the given item). If you're asking whether the comparer function can compare multiple properties of the two elements in order to determine "equality", then yes it can. For example:
var arr = [{id: 1, make: 'Honda', model: 'Civic'}, {id: 2, make: 'Acura', model: 'MDX'}];

// Note that the comparer does not take the 'id' property into consideration when comparing.
var hasElement = $linq(arr).contains({make: 'Honda', model: 'Civic'}, 
    function (x, y) { return (x.make == y.make) && (x.model == y.model); });
A related function is the "any" function--it returns true if any element of the collection satisfies the predicate function passed to the "any" function. So, if you had a set of conditions that indicated whether a given element is "contained" in the collection, you could use the "any" function to test this, passing a predicate function that represented the conditions. For example:
var arr = [{id: 1, name: 'Jason'}, {id: 2, name: 'Kent'}, {id: 3, name: 'Jason'}];

// Test whether the collection contains an element whose:
//    (1) id is greater than or equal to 2, and
//    (2) name is 'Jason'
// The following linq query would return 'true' with the third element satisfying
// the conditions.
if ($linq(arr).any(function (x) { return (x.id >= 2) && (x.name == 'Jason'); }))
    // further code...