Aggregation
Aggregate, Average, Count, LongCount, Max, Min, Sum
Conversion
AsEnumerable, Cast, OfType, ToArray, ToDictionary, ToList, ToLookup
Element
ElementAt, ElementAtOrDefault, First, FirstOrDefault, Last, LastOrDefault, Single, SingleOrDefault
ElementAt
ElementAtOrDefault
First (simple)
First (conditional)
FirstOrDefault
Last
LastOrDefault (simple)
LastOrDefault (conditional)
Single
SingleOrDefault
Generation
DefaultIfEmpty, Empty, Range, Repeat
Grouping
GroupBy
Join
GroupJoin, Join
Ordering
OrderBy, OrderByDescending, Reverse, ThenBy, ThenByDescending
OrderBy (simple - numbers)
OrderBy (simple - dates)
OrderBy (simple - objects)
OrderByDescending
Reverse
ThenBy
ThenByDescending
Other
Concat, SequenceEqual, Zip
Partitioning
Skip, SkipWhile, Take, TakeWhile
Projection
Select, SelectMany
Quantifiers
All, Any, Contains
Restriction
Where
Set
Distinct, Except, Intersect, Union
Close
DefaultIfEmpty: If a collection is empty, its default value is returned. Default value depends on collection type.
DefaultIfEmpty: If a collection is empty, its default value is returned. Default value depends on collection type. |
DefaultIfEmpty: If a collection is empty, its default value is returned. Default value depends on collection type. |
This Query Expression sample returns default values for each of the empty collections, while "words" array is returned as is. |
This Query Expression sample returns default values for each of the empty collections, while "words" array is returned as is. |
// A Query Expression cannot be constructed for DefaultIfEmpty() in C#. // Consider using a Lambda Expression instead.
' A Query Expression cannot be constructed for DefaultIfEmpty() in VB.NET. ' Consider using a Lambda Expression instead.
// A Query Expression cannot be constructed for DefaultIfEmpty() in F#. // Consider using a Lambda Expression instead.
static void Sample_DefaultIfEmpty_Lambda_Simple() { string[] emptyStr = { }; int[] emptyInt = { }; string[] words = { "one", "two", "three" }; var resultStr = emptyStr.DefaultIfEmpty(); // Default is null for string var resultInt = emptyInt.DefaultIfEmpty(); // Default is 0 for int var resultWords = words.DefaultIfEmpty(); // Not empty, so words array is just copied Debug.WriteLine("resultStr has one element with a value of null:"); Debug.WriteLine(resultStr.Count() == 1 && resultStr.First() == null); Debug.WriteLine("resultInt has one element with a value of 0:"); Debug.WriteLine(resultInt.Count() == 1 && resultInt.First() == 0); Debug.WriteLine("resultWords has same content as words array:"); Debug.WriteLine(resultWords.SequenceEqual(words)); }Output:
resultStr has one element with a value of null: True resultInt has one element with a value of 0: True resultWords has same content as words array: True
Private Shared Sub Sample_DefaultIfEmpty_Lambda_Simple() Dim emptyStr As String() = {} Dim emptyInt As Integer() = {} Dim words As String() = {"one", "two", "three"} Dim resultStr = emptyStr.DefaultIfEmpty() ' Default is null for string Dim resultInt = emptyInt.DefaultIfEmpty() ' Default is 0 for int Dim resultWords = words.DefaultIfEmpty() ' Not empty, so words array is just copied Debug.WriteLine("resultStr has one element with a value of null:") Debug.WriteLine(resultStr.Count() = 1 AndAlso resultStr.First() Is Nothing) Debug.WriteLine("resultInt has one element with a value of 0:") Debug.WriteLine(resultInt.Count() = 1 AndAlso resultInt.First() = 0) Debug.WriteLine("resultWords has same content as words array:") Debug.WriteLine(resultWords.SequenceEqual(words)) End SubOutput:
resultStr has one element with a value of null: True resultInt has one element with a value of 0: True resultWords has same content as words array: True
let Sample_DefaultIfEmpty_Lambda_Simple() = let emptyStr:System.String list = [] let emptyInt:System.Int32 list = [] let words = [|"one"; "two"; "three"|] let resultStr = emptyStr.DefaultIfEmpty() // Default is null for string let resultInt = emptyInt.DefaultIfEmpty() // Default is 0 for int let resultWords = words.DefaultIfEmpty() // Not empty, so words array is just copied Debug.WriteLine(sprintf "resultStr has one element with a value of null:") Debug.WriteLine(sprintf "%b" (resultStr.Count() = 1 && resultStr.First() = null)) Debug.WriteLine(sprintf "resultInt has one element with a value of 0:") Debug.WriteLine(sprintf "%b" (resultInt.Count() = 1 && resultInt.First() = 0)) Debug.WriteLine("resultWords has same content as words array:") Debug.WriteLine(sprintf "%b" (resultWords.SequenceEqual(words)))Output:
resultStr has one element with a value of null: true resultInt has one element with a value of 0: true resultWords has same content as words array: true