-
Notifications
You must be signed in to change notification settings - Fork 3
SingleOrDefault
Yasser Moradi edited this page Aug 26, 2015
·
2 revisions
Description:
This method similar to single method . However if the sequence or result of predicate function is empty, it returns null instead of throwing exception.
Sample:
let singleList = ['one'];
let items = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten'];
let singleOrDefault = singleList.asEnumerable().singleOrDefault();
console.log(singleOrDefault);
try {
singleOrDefault = items.asEnumerable().singleOrDefault();
}
catch (err) {
console.error(err.message);
}
singleOrDefault = items.asEnumerable().singleOrDefault(item => item.indexOf('E') === 0);
console.log(singleOrDefault);
singleOrDefault = items.asEnumerable().singleOrDefault(item => item.indexOf('X') === 0);
console.log(singleOrDefault);
try {
singleOrDefault = items.asEnumerable().singleOrDefault(item => item.indexOf('F') === 0);
}
catch (err) {
console.error(err.message);
}