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

ThenBy

Lambda Query

ThenBy: Use after earlier sorting, to further sort a collection in ascending order.

ThenBy: Use after earlier sorting, to further sort a collection in ascending order.

This Query Expression sample first sorts array by string length of city capital, and then by alphabet.

This Query Expression sample first sorts array by string length of city capital, and then by alphabet.

  • VB.Net
  • F#
  • C#
Private Shared Sub Sample_ThenBy_Linq()
    Dim capitals As String() = {"Berlin", "Paris", "Madrid", "Tokyo", "London", "Athens", _
        "Beijing", "Seoul"}

    Dim result = (From c In capitals Order By c.Length).ThenBy(Function(c) c)

    Debug.WriteLine("Ordered list of capitals, first by length and then alphabetical:")
    For Each capital As String In result
        Debug.WriteLine(capital)
    Next
End Sub
Output:
Ordered list of capitals, first by length and then alphabetical:
Paris
Seoul
Tokyo
Athens
Berlin
London
Madrid
Beijing
let Sample_ThenBy_Linq() =
    let capitals = [|"Berlin"; "Paris"; "Madrid"; "Tokyo"; "London"; 
                          "Athens"; "Beijing"; "Seoul"|]

    let result = query {
        for c in capitals do
        sortBy c.Length
        thenBy c
    }

    Debug.WriteLine(sprintf "Ordered list of capitals, first by length and then alphabetical:")
    for capital in result do
        Debug.WriteLine(sprintf "%s" capital)
Output:
Ordered list of capitals, first by length and then alphabetical:
Paris
Seoul
Tokyo
Athens
Berlin
London
Madrid
Beijing
static void Sample_ThenBy_Linq()
{
    string[] capitals = { "Berlin", "Paris", "Madrid", "Tokyo", "London", "Athens", "Beijing", "Seoul" };

    var result = (from c in capitals
                  orderby c.Length
                  select c)
                 .ThenBy(c => c);

    Debug.WriteLine("Ordered list of capitals, first by length and then alphabetical:");
    foreach (string capital in result)
        Debug.WriteLine(capital);
}
Output:
Ordered list of capitals, first by length and then alphabetical:
Paris
Seoul
Tokyo
Athens
Berlin
London
Madrid
Beijing
  • C#
  • F#
  • VB.Net
static void Sample_ThenBy_Lambda()
{
    string[] capitals = { "Berlin", "Paris", "Madrid", "Tokyo", "London", 
                          "Athens", "Beijing", "Seoul" };

    var result = capitals.OrderBy(c => c.Length).ThenBy(c => c);

    Debug.WriteLine("Ordered list of capitals, first by length and then alphabetical:");
    foreach (string capital in result)
        Debug.WriteLine(capital);
}
Output:
Ordered list of capitals, first by length and then alphabetical:
Paris
Seoul
Tokyo
Athens
Berlin
London
Madrid
Beijing
let Sample_ThenBy_Lambda() =
    let capitals = [|"Berlin"; "Paris"; "Madrid"; "Tokyo"; "London"; 
                          "Athens"; "Beijing"; "Seoul"|]

    let result = capitals.OrderBy(fun c -> c.Length).ThenBy(fun c -> c)

    Debug.WriteLine(sprintf "Ordered list of capitals, first by length and then alphabetical:")
    for capital in result do
        Debug.WriteLine(sprintf "%s" capital)
Output:
Ordered list of capitals, first by length and then alphabetical:
Paris
Seoul
Tokyo
Athens
Berlin
London
Madrid
Beijing
Private Shared Sub Sample_ThenBy_Lambda()
    Dim capitals As String() = {"Berlin", "Paris", "Madrid", "Tokyo", "London", "Athens", _
        "Beijing", "Seoul"}

    Dim result = capitals.OrderBy(Function(c) c.Length).ThenBy(Function(c) c)

    Debug.WriteLine("Ordered list of capitals, first by length and then alphabetical:")
    For Each capital As String In result
        Debug.WriteLine(capital)
    Next
End Sub
Output:
Ordered list of capitals, first by length and then alphabetical:
Paris
Seoul
Tokyo
Athens
Berlin
London
Madrid
Beijing

Share this sample on:

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