defaultIfEmpty()

The defaultIfEmpty linq operator returns a new linq object that contains either the same elements as the original collection or, if the original collection is empty, the given "defaultValue" as the only element. In other words, this linq operator always returns a collection with at least one element.
var arr = [1, 2, 3, 4, 5];

// col1 = [1, 2, 3, 4, 5]
var col1 = $linq(arr).defaultIfEmpty(0).toArray();

// col2 = [0]
var col2 = $linq([]).defaultIfEmpty(0).toArray();
Also, see the documentation for groupJoin for an example of using defaultIfEmpty to help deal with the "null" parts of the outer side of the join in that example.

Last edited Sep 25, 2012 at 1:39 AM by battousai999, version 2