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
OrderBy: Sorts a collection in ascending order.
OrderBy: Sorts a collection in ascending order. |
OrderBy: Sorts a collection in ascending order. |
This Lambda Expression sample sorts array of cars by "HorsePower", in ascending order. |
This Lambda Expression sample sorts array of cars by "HorsePower", in ascending order. |
class Car { public string Name { get; set; } public int HorsePower { get; set; } } static void Sample_OrderBy_Linq_Objects() { Car[] cars = { new Car { Name = "Super Car", HorsePower = 215 }, new Car { Name = "Economy Car", HorsePower = 75 }, new Car { Name = "Family Car", HorsePower = 145 }, }; var result = from c in cars orderby c.HorsePower select c; Debug.WriteLine("Ordered list of cars by horsepower using Query Syntax:"); foreach (Car car in result) Debug.WriteLine(String.Format("{0}: {1} horses", car.Name, car.HorsePower)); }Output:
Ordered list of cars by horsepower using Query Syntax: Economy Car: 75 horses Family Car: 145 horses Super Car: 215 horses
Class Car Public Property Name() As String Public Property HorsePower() As Integer End Class Private Shared Sub Sample_OrderBy_Linq_Objects() Dim cars As Car() = { New Car() With { _ .Name = "Super Car", .HorsePower = 215 _ }, New Car() With { _ .Name = "Economy Car", .HorsePower = 75 _ }, New Car() With { _ .Name = "Family Car", .HorsePower = 145 _ } } Dim result = From c In cars Order By c.HorsePower Debug.WriteLine("Ordered list of cars by horsepower using Query Syntax:") For Each car As Car In result Debug.WriteLine(String.Format("{0}: {1} horses", car.Name, car.HorsePower)) Next End SubOutput:
Ordered list of cars by horsepower using Query Syntax: Economy Car: 75 horses Family Car: 145 horses Super Car: 215 horses
// There is no OrderBy() for Query Expressions in F#, but it has the similar "sortBy". type Car(name, horsePower) = member val Name = name member val HorsePower = horsePower let Sample_OrderBy_Linq_Objects() = let cars = [| new Car("Super Car", 215); new Car("Economy Car", 75); new Car("Family Car", 145) |] let result = query { for c in cars do sortBy c.HorsePower } Debug.WriteLine(sprintf "Ordered list of cars by horsepower:") for car in result do Debug.WriteLine(sprintf "%s: %d horses" car.Name car.HorsePower)Output:
Ordered list of cars by horsepower: Economy Car: 75 horses Family Car: 145 horses Super Car: 215 horses
class Car { public string Name { get; set; } public int HorsePower { get; set; } } static void Sample_OrderBy_Lambda_Objects() { Car[] cars = { new Car { Name = "Super Car", HorsePower = 215 }, new Car { Name = "Economy Car", HorsePower = 75 }, new Car { Name = "Family Car", HorsePower = 145 }, }; var result = cars.OrderBy(c => c.HorsePower); Debug.WriteLine("Ordered list of cars by horsepower:"); foreach (Car car in result) Debug.WriteLine(String.Format("{0}: {1} horses", car.Name, car.HorsePower)); }Output:
Ordered list of cars by horsepower: Economy Car: 75 horses Family Car: 145 horses Super Car: 215 horses
Class Car Public Property Name() As String Public Property HorsePower() As Integer End Class Private Shared Sub Sample_OrderBy_Lambda_Objects() Dim cars As Car() = { New Car() With { _ .Name = "Super Car", .HorsePower = 215 _ }, New Car() With { _ .Name = "Economy Car", .HorsePower = 75 _ }, New Car() With { _ .Name = "Family Car", .HorsePower = 145 _ } } Dim result = cars.OrderBy(Function(c) c.HorsePower) Debug.WriteLine("Ordered list of cars by horsepower:") For Each car As Car In result Debug.WriteLine([String].Format("{0}: {1} horses", car.Name, car.HorsePower)) Next End SubOutput:
Ordered list of cars by horsepower: Economy Car: 75 horses Family Car: 145 horses Super Car: 215 horses
type Car(name, horsePower) = member val Name = name member val HorsePower = horsePower let Sample_OrderBy_Lambda_Objects() = let cars = [| new Car("Super Car", 215); new Car("Economy Car", 75); new Car("Family Car", 145) |] let result = cars.OrderBy(fun c -> c.HorsePower) Debug.WriteLine(sprintf "Ordered list of cars by horsepower:") for car in result do Debug.WriteLine(sprintf "%s: %d horses" car.Name car.HorsePower)Output:
Ordered list of cars by horsepower: Economy Car: 75 horses Family Car: 145 horses Super Car: 215 horses