LINQSamples.com
  • Samples
  • Tutorials
  • About
  • Links
  • Contact
LINQ to Objects

Aggregation

Aggregate, Average, Count, LongCount, Max, Min, Sum

      Aggregate (simple)       Aggregate (seed)       Average       Count       LongCount       Max       Min       Sum

Conversion

AsEnumerable, Cast, OfType, ToArray, ToDictionary, ToList, ToLookup

      AsEnumerable       Cast       OfType       ToArray       ToDictionary (simple)       ToDictionary (conditional)       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

      DefaultIfEmpty (simple)       DefaultIfEmpty (default value)       Empty       Range       Repeat

Grouping

GroupBy

      GroupBy

Join

GroupJoin, Join

      GroupJoin (left outer join)       Join (inner join)

Ordering

OrderBy, OrderByDescending, Reverse, ThenBy, ThenByDescending

      OrderBy (simple - numbers)       OrderBy (simple - dates)       OrderBy (simple - objects)       OrderByDescending       Reverse       ThenBy       ThenByDescending

Other

Concat, SequenceEqual, Zip

      Concat (simple - numbers)       Concat (simple - strings)       SequenceEqual       Zip

Partitioning

Skip, SkipWhile, Take, TakeWhile

      Skip       SkipWhile       Take       TakeWhile

Projection

Select, SelectMany

      Select (simple)       Select (anonymous type)       Select (indexed)       SelectMany (cross join)

Quantifiers

All, Any, Contains

      All       Any       Contains

Restriction

Where

      Where (simple - numbers)       Where (simple - objects)       Where (indexed)

Set

Distinct, Except, Intersect, Union

      Distinct       Except       Intersect       Union
  Close

LastOrDefault (cond.)

Lambda Query

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 having a length of 3 and 2 respectively. As no elements have a length of 2, "resultNoMatch" array gets default value (null).

This Lambda 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).

  • C#
  • VB.Net
  • F#
// 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.
  • C#
  • VB.Net
  • F#
// 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 Sub
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.
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

Share this sample on:

© 2023 - LINQSamples.com | Terms Of Service  -  Privacy Policy