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

ElementAt

Lambda Query

ElementAt: Retrieves element from a collection at specified (zero-based) index.

ElementAt: Retrieves element from a collection at specified (zero-based) index.

This Lambda Expression sample retrieves second element from an array.

This Lambda Expression sample retrieves second element from an array.

  • C#
  • VB.Net
  • F#
// A Query Expression cannot be constructed for ElementAt() in C#.
// Consider using a Lambda Expression instead.
' A Query Expression cannot be constructed for ElementAt() in VB.NET.
' Consider using a Lambda Expression instead.
// There is no ElementAt() for Query Expressions in F#, but it has the similar "nth".
let Sample_ElementAt_Linq() =
    let words = [|"One"; "Two"; "Three"|]

    let result = query {
        for w in words do
        nth 1
    }

    Debug.WriteLine(sprintf "Element at index 1 in the array is:")
    Debug.WriteLine(sprintf "%s" result)
Output:
Element at index 1 in the array is:
Two
  • C#
  • VB.Net
  • F#
static void Sample_ElementAt_Lambda()
{
    string[] words = { "One", "Two", "Three" };

    var result = words.ElementAt(1);

    Debug.WriteLine("Element at index 1 in the array is:");
    Debug.WriteLine(result);
}
Output:
Element at index 1 in the array is:
Two
Private Shared Sub Sample_ElementAt_Lambda()
    Dim words As String() = {"One", "Two", "Three"}

    Dim result = words.ElementAt(1)

    Debug.WriteLine("Element at index 1 in the array is:")
    Debug.WriteLine(result)
End Sub
Output:
Element at index 1 in the array is:
Two
let Sample_ElementAt_Lambda() =
    let words = [|"One"; "Two"; "Three"|]

    let result = words.ElementAt(1)

    Debug.WriteLine(sprintf "Element at index 1 in the array is:")
    Debug.WriteLine(sprintf "%s" result)
Output:
Element at index 1 in the array is:
Two

Share this sample on:

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