first()

The first linq operator returns the either (1) the first element of the collection (if no predicate function is given), or (2) the first element in the collection that satisfies the given predicate function. The first operator will throw an exception if the collection is empty (when a predicate is not given) or if there is no element that satisfies the given predicate.
var arr = [{name: 'John', age: 27},
    {name: 'Allen', age: 20},
    {name: 'Peter', age: 18},
    {name: 'Susan', age: 23}];
    
// name1 = 'John'
var name1 = $linq(arr).first().name;

// name2 = 'Peter'
var name2 = $linq(arr).first("x => x.age < 20").name;

firstOrDefault()

The firstOrDefault linq operator behaves like the first operator, except that the firstOrDefault operator will return the given default value if the collection is empty (when a predicate is not given) or if there is no element that satisfies the given predicate.
var arr = [{name: 'John', age: 27},
    {name: 'Allen', age: 20},
    {name: 'Peter', age: 18},
    {name: 'Susan', age: 23}];
    
// name1 = 'none'
var name1 = $linq([]).firstOrDefault({name: "none"}).name;

// name2 = 'no one'
var name2 = $linq(arr).firstOrDefault({name: "no one"}, "x => x.age < 10").name;

single()

The single linq operator returns either (1) the only element in the collection (if no predicate function is given), or (2) the only element in the collection that satisfies the given predicate. If no predicate is given, then the single operator will throw an exception if either the collection is empty or if there are more than one element in the collection (i.e., there is not a "single" element). If a predicate is given, then the single operator will throw an exception if either there is no element that satisfies the predicate or if there is more than one element that satisfies the predicate.
var arr = [{name: 'John', age: 27},
    {name: 'Allen', age: 20},
    {name: 'Peter', age: 18},
    {name: 'Susan', age: 23}];
    
// val1 = 1
var val1 = $linq([1]).single();

// name1 = 'Peter'
var name1 = $linq(arr).single("x => x.age == 18").name;

singleOrDefault()

The singleOrDefault linq operator behaves like the single operator, except that it does not throw an exception if there are no elements in the collection (if no predicate is given) or if there are no elements in the collection that satisfy the given predicate--in these cases, the given "default value" is returned, instead.

Note, that the singleOrDefault operator will still throw an exception if there are more than one element in the collection (if no predicate is given) or if there are more than one element that satisfy the given predicate.
var arr = [{name: 'John', age: 27},
    {name: 'Allen', age: 20},
    {name: 'Peter', age: 18},
    {name: 'Susan', age: 23}];
    
// val1 = 3
var val1 = $linq([]).singleOrDefault(3);

// name1 = 'multiple'
var name1 = $linq(arr).singleOrDefault({name: 'multiple'}, "x => x.age >= 27").name;

singleOrFallback()

The singleOrFallback linq operator behaves like the single operator, except that it does not throw an exception if there are no elements in the collection--in this case, the value returned is determined by evaluating the "fallback" function.

Note, that the singleOrFallback operator will still throw an exception if there is more than one element in the collection.
var arr = [{name: 'John', age: 27},
    {name: 'Allen', age: 20},
    {name: 'Peter', age: 18},
    {name: 'Susan', age: 23}];
    
// val1 = 4
var val1 = $linq([]).singleOrFallback("() => arr.length");

last()

The last linq operator returns either (1) the last element in the collection (if no predicate function is given), or (2) the last element in the collection that satisfies the given predicate. The last operator will throw an exception if the collection is empty (when a predicate is not given) or if there is no element that satisfies the given predicate.
var arr = [{name: 'John', age: 27},
    {name: 'Allen', age: 20},
    {name: 'Peter', age: 18},
    {name: 'Susan', age: 23}];
    
// name1 = 'Susan'
var name1 = $linq(arr).last().name;

// name2 = 'Peter'
var name2 = $linq(arr).last("x => x.age < 22").name;

lastOrDefault()

The lastOrDefault linq operator behaves like the last operator, except that the lastOrDefault operator will return the given default value if the collection is empty (when a predicate is not given) or if there is no element that satisfies the given predicate.
var arr = [{name: 'John', age: 27},
    {name: 'Allen', age: 20},
    {name: 'Peter', age: 18},
    {name: 'Susan', age: 23}];
    
// name1 = 'none'
var name1 = $linq([]).lastOrDefault({name: "none"}).name;

// name2 = 'no one'
var name2 = $linq(arr).lastOrDefault({name: "no one"}, "x => x.age < 10").name;

Last edited Apr 30, 2013 at 7:34 AM by battousai999, version 6