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
LastOrDefault: Retrieves last element from a collection, or default value if collection is empty.
LastOrDefault: Retrieves last element from a collection, or default value if collection is empty. |
LastOrDefault: Retrieves last element from a collection, or default value if collection is empty. |
This Lambda Expression sample retrieves last element from words array, but from empty array it gets default value (null). |
This Lambda Expression sample retrieves last element from words array, but from empty array it gets default value (null). |
// A Query Expression cannot be constructed for LastOrDefault() in C#. // Consider using a Lambda Expression instead.
' A Query Expression cannot be constructed for LastOrDefault() in VB.NET. ' Consider using a Lambda Expression instead.
// A Query Expression cannot be constructed for LastOrDefault() in F#. // Consider using a Lambda Expression instead.
// Note: While Last will throw an exception if array... // ...is empty, LastOrDefault gracefully returns null. static void Sample_LastOrDefault_Simple() { string[] words = { "one", "two", "three" }; string[] empty = { }; var result = words.LastOrDefault(); var resultEmpty = empty.LastOrDefault(); Debug.WriteLine("Last element in the words array contains:"); Debug.WriteLine(result); Debug.WriteLine("Last element in the empty array does not exist:"); Debug.WriteLine(resultEmpty == null); }Output:
Last element in the words array contains: three Last element in the empty array does not exist: True
' Note: While Last will throw an exception if array... ' ...is empty, LastOrDefault gracefully returns null. Private Shared Sub Sample_LastOrDefault_Simple() Dim words As String() = {"one", "two", "three"} Dim empty As String() = {} Dim result = words.LastOrDefault() Dim resultEmpty = empty.LastOrDefault() Debug.WriteLine("Last element in the words array contains:") Debug.WriteLine(result) Debug.WriteLine("Last element in the empty array does not exist:") Debug.WriteLine(resultEmpty Is Nothing) End SubOutput:
Last element in the words array contains: three Last element in the empty array does not exist: True
// Note: While Last will throw an exception if array... // ...is empty, LastOrDefault gracefully returns null. let Sample_LastOrDefault_Simple() = let words = [|"one"; "two"; "three"|] let empty = [||] let result = words.LastOrDefault() let resultEmpty = empty.LastOrDefault() Debug.WriteLine(sprintf "Last element in the words array contains:") Debug.WriteLine(sprintf "%s" result) Debug.WriteLine(sprintf "Last element in the empty array does not exist:") Debug.WriteLine(sprintf "%b" (resultEmpty = null))Output:
Last element in the words array contains: three Last element in the empty array does not exist: true