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 Query Expression sample retrieves last element from "words" array having a length of 3 and 2 respectively. As no elements have a length of 2, "resultNoMatch" array gets default value (null). |
This Query Expression sample retrieves last element from "words" array having a length of 3 and 2 respectively. As no elements have a length of 2, "resultNoMatch" array 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_Conditional() { string[] words = { "one", "two", "three" }; var result = words.LastOrDefault(w => w.Length == 3); var resultNoMatch = words.LastOrDefault(w => w.Length == 2); Debug.WriteLine("Last element in the words array having a length of 3:"); Debug.WriteLine(result); Debug.WriteLine("Last element in the empty array having a length of 2 does not exist:"); Debug.WriteLine(resultNoMatch == null); }Output:
Last element in the words array having a length of 3: two Last element in the empty array having a length of 2 does not exist: True
' Note: While Last will throw an exception if array... ' ...is empty, LastOrDefault gracefully returns null. Private Shared Sub Sample_LastOrDefault_Conditional() Dim words As String() = {"one", "two", "three"} Dim result = words.LastOrDefault(Function(w) w.Length = 3) Dim resultNoMatch = words.LastOrDefault(Function(w) w.Length = 2) Debug.WriteLine("Last element in the words array having a length of 3:") Debug.WriteLine(result) Debug.WriteLine("Last element in the empty array having a length of 2 does not exist:") Debug.WriteLine(resultNoMatch Is Nothing) End SubOutput:
Last element in the words array having a length of 3: two Last element in the empty array having a length of 2 does not exist: True
// Note: While Last will throw an exception if array... // ...is empty, LastOrDefault gracefully returns null. let Sample_LastOrDefault_Conditional() = let words = [|"one"; "two"; "three"|] let result = words.LastOrDefault(fun w -> w.Length = 3) let resultEmpty = words.LastOrDefault(fun w -> w.Length = 2) Debug.WriteLine(sprintf "Last element in the words array having a length of 3:") Debug.WriteLine(sprintf "%s" result) Debug.WriteLine(sprintf "Last element in the empty array having a length of 2 does not exist:") Debug.WriteLine(sprintf "%b" (resultEmpty = null))Output:
Last element in the words array having a length of 3: two Last element in the empty array having a length of 2 does not exist: true