Skip to content
Yasser Moradi edited this page Aug 26, 2015 · 4 revisions

Description:

This method takes default value as an argument and a source. Then it checks if source is empty, returns default value, otherwise it returns source.

DefaultIfEmpty, can be used in LeftJoin queries.

Sample:

let items = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten'];

let defaultIfEmpty = items.asEnumerable().defaultIfEmpty().toArray();
console.log(defaultIfEmpty);

Description:

If the source is empty , a new source will be created and new item will be added to it. If default value argument is not null, the new item will be a passed default value, otherwise new item will be undefined.

Sample:

let empty = [];
let items = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten'];

let defaultIfEmpty = empty.asEnumerable().defaultIfEmpty('default value').toArray();
console.log(defaultIfEmpty);

defaultIfEmpty = empty.asEnumerable().defaultIfEmpty().toArray();
console.log(defaultIfEmpty);
Clone this wiki locally