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

ElementAtOrDefault

Lambda Query

ElementAtOrDefault: Retrieves element from a collection at specified (zero-based) index, but gets default value if out-of-range.

ElementAtOrDefault: Retrieves element from a collection at specified (zero-based) index, but gets default value if out-of-range.

This Lambda Expression sample retrieves elements at index 1 and 10 from array, and because index 10 is out-of-range, it gets default value (null).

This Lambda Expression sample retrieves elements at index 1 and 10 from array, and because index 10 is out-of-range, it gets default value (null).

  • C#
  • VB.Net
  • F#
// A Query Expression cannot be constructed for ElementAtOrDefault() in C#.
// Consider using a Lambda Expression instead.
' A Query Expression cannot be constructed for ElementAtOrDefault() in VB.NET.
' Consider using a Lambda Expression instead.
// A Query Expression cannot be constructed for ElementAtOrDefault() in F#.
// Consider using a Lambda Expression instead.
  • C#
  • VB.Net
  • F#
static void Sample_ElementAtOrDefault_Lambda()
{
    string[] colors = { "Red", "Green", "Blue" };

    var resultIndex1 = colors.ElementAtOrDefault(1);

    var resultIndex10 = colors.ElementAtOrDefault(10);

    Debug.WriteLine("Element at index 1 in the array contains:");
    Debug.WriteLine(resultIndex1);

    Debug.WriteLine("Element at index 10 in the array does not exist:");
    Debug.WriteLine(resultIndex10 == null);
}
Output:
Element at index 1 in the array contains:
Green
Element at index 10 in the array does not exist:
True
Private Shared Sub Sample_ElementAtOrDefault_Lambda()
    Dim colors As String() = {"Red", "Green", "Blue"}

    Dim resultIndex1 = colors.ElementAtOrDefault(1)

    Dim resultIndex10 = colors.ElementAtOrDefault(10)

    Debug.WriteLine("Element at index 1 in the array contains:")
    Debug.WriteLine(resultIndex1)

    Debug.WriteLine("Element at index 10 in the array does not exist:")
    Debug.WriteLine(resultIndex10 Is Nothing)
End Sub
Output:
Element at index 1 in the array contains:
Green
Element at index 10 in the array does not exist:
True
let Sample_ElementAtOrDefault_Lambda() =
    let colors = [|"Red"; "Green"; "Blue"|]

    let resultIndex1 = colors.ElementAtOrDefault(1)

    let resultIndex10 = colors.ElementAtOrDefault(10)

    Debug.WriteLine("Element at index 1 in the array contains:");
    Debug.WriteLine(sprintf "%s" resultIndex1);

    Debug.WriteLine("Element at index 10 in the array does not exist:")
    Debug.WriteLine(sprintf "%b" (resultIndex10 = null))
Output:
Element at index 1 in the array contains:
Green
Element at index 10 in the array does not exist:
true

Share this sample on:

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