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

ThenByDescending

Lambda Query

ThenByDescending: Use after earlier sorting, to further sort a collection in descending order.

ThenByDescending: Use after earlier sorting, to further sort a collection in descending order.

This Lambda Expression sample first orders a list of dates by year descending, and then by month descending.

This Lambda Expression sample first orders a list of dates by year descending, and then by month descending.

  • C#
  • VB.Net
  • F#
static void Sample_ThenByDescending_Linq()
{
    var dates = new DateTime[] {
        new DateTime(2015, 3, 1),
        new DateTime(2014, 7, 1),
        new DateTime(2013, 5, 1),
        new DateTime(2015, 1, 1),
        new DateTime(2015, 7, 1)
    };

    var result = from d in dates
                 orderby d.Year descending, d.Month descending
                 select d;

    Debug.WriteLine("List of dates first ordered by year descending, and then by month descending:");
    foreach (DateTime dt in result)
        Debug.WriteLine(dt.ToString("yyyy/MM/dd"));
}
Output:
List of dates first ordered by year descending, and then by month descending:
2015-07-01
2015-03-01
2015-01-01
2014-07-01
2013-05-01
Private Shared Sub Sample_ThenByDescending_Linq()
    Dim dates = New DateTime() { _
        New DateTime(2015, 3, 1), _
        New DateTime(2014, 7, 1), _
        New DateTime(2013, 5, 1), _
        New DateTime(2015, 1, 1), _
        New DateTime(2015, 7, 1)
    }

    Dim result = From d In dates _
                 Order By d.Year Descending, d.Month Descending
                 Select d

    Debug.WriteLine("List of dates first ordered by year descending, and then by month descending:")
    For Each dt As DateTime In result
        Debug.WriteLine(dt.ToString("yyyy/MM/dd"))
    Next
End Sub
Output:
List of dates first ordered by year descending, and then by month descending:
2015-07-01
2015-03-01
2015-01-01
2014-07-01
2013-05-01
let Sample_ThenByDescending_Linq() =
    let dates = [|new DateTime(2015, 3, 1); 
                  new DateTime(2014, 7, 1); 
                  new DateTime(2013, 5, 1); 
                  new DateTime(2015, 1, 1); 
                  new DateTime(2015, 7, 1)|]

    let result = query {
        for d in dates do
        sortByDescending d.Year
        thenByDescending d.Month
        select d
    }

    Debug.WriteLine(sprintf "List of dates first ordered by year descending, and then by month descending:");
    for dt in result do
        Debug.WriteLine(dt.ToString("yyyy/MM/dd"))
Output:
List of dates first ordered by year descending, and then by month descending:
2015-07-01
2015-03-01
2015-01-01
2014-07-01
2013-05-01
  • C#
  • VB.Net
  • F#
static void Sample_ThenByDescending_Lambda()
{
    var dates = new DateTime[] {
        new DateTime(2015, 3, 1),
        new DateTime(2014, 7, 1),
        new DateTime(2013, 5, 1),
        new DateTime(2015, 1, 1),
        new DateTime(2015, 7, 1)
    };

    var result = dates.OrderByDescending(d => d.Year).ThenByDescending(d => d.Month);

    Debug.WriteLine("List of dates first ordered by year descending, and then by month descending:");
    foreach (DateTime dt in result)
        Debug.WriteLine(dt.ToString("yyyy/MM/dd"));
}
Output:
List of dates first ordered by year descending, and then by month descending:
2015-07-01
2015-03-01
2015-01-01
2014-07-01
2013-05-01
Private Shared Sub Sample_ThenByDescending_Lambda()
    Dim dates = New DateTime() { _
        New DateTime(2015, 3, 1), _
        New DateTime(2014, 7, 1), _
        New DateTime(2013, 5, 1), _
        New DateTime(2015, 1, 1), _
        New DateTime(2015, 7, 1)
    }

    Dim result = dates.OrderByDescending(Function(d) d.Year).ThenByDescending(Function(d) d.Month)

    Debug.WriteLine("List of dates first ordered by year descending, and then by month descending:")
    For Each dt As DateTime In result
        Debug.WriteLine(dt.ToString("yyyy/MM/dd"))
    Next
End Sub
Output:
List of dates first ordered by year descending, and then by month descending:
2015-07-01
2015-03-01
2015-01-01
2014-07-01
2013-05-01
let Sample_ThenByDescending_Lambda() =
    let dates = [|new DateTime(2015, 3, 1); 
                  new DateTime(2014, 7, 1); 
                  new DateTime(2013, 5, 1); 
                  new DateTime(2015, 1, 1); 
                  new DateTime(2015, 7, 1)|]

    let result = dates.OrderByDescending(fun d -> d.Year).ThenByDescending(fun d -> d.Month)

    Debug.WriteLine(sprintf "List of dates first ordered by year descending, and then by month descending:");
    for dt in result do
        Debug.WriteLine(dt.ToString("yyyy/MM/dd"))
Output:
List of dates first ordered by year descending, and then by month descending:
2015-07-01
2015-03-01
2015-01-01
2014-07-01
2013-05-01

Share this sample on:

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