Aggregation
Aggregate, Average, Count, LongCount, Max, Min, Sum
Conversion
AsEnumerable, Cast, OfType, ToArray, ToDictionary, 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
Grouping
GroupBy
Join
GroupJoin, Join
Ordering
OrderBy, OrderByDescending, Reverse, ThenBy, ThenByDescending
OrderBy (simple - numbers)
OrderBy (simple - dates)
OrderBy (simple - objects)
OrderByDescending
Reverse
ThenBy
ThenByDescending
Other
Concat, SequenceEqual, Zip
Partitioning
Skip, SkipWhile, Take, TakeWhile
Projection
Select, SelectMany
Quantifiers
All, Any, Contains
Restriction
Where
Set
Distinct, Except, Intersect, Union
Close
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. |
ThenBy: Use after earlier sorting, to further sort a collection in ascending order. |
This Lambda Expression sample first sorts array by string length of city capital, and then by alphabet. |
This Lambda Expression sample first sorts array by string length of city capital, and then by alphabet. |
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
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 SubOutput:
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_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
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 SubOutput:
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