Can somebody explain how DefaultIfEmpty() can be used in LINQ. I have ready some material but still need something solid to see what the use of it is.<
DefaultIfEmpty()
It basically returns a collection with a single element in case the source collection is empty.
var numbers = new int[] {1, 2, 3}; var aNumber = numbers.First();
returns 1
but
var numbers = new int[]; var aNumber = numbers.DefaultIfEmpty(12).Single();
returns 12 as the collection is empty