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

OrderBy (simple - objects)

Lambda Query

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.

  • C#
  • VB.Net
  • F#
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 Sub
Output:
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
  • C#
  • VB.Net
  • F#
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 Sub
Output:
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

Share this sample on:

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