Core Concepts > Basic Queries

Top

Use the Top method while composing a query to limit the number of results affected/returned from execution.

//select the top 5 purchases by dollar amount
IEnumerable<Purchase> purchases = db.SelectMany<Purchase>()
    .Top(5)
    .From(dbo.Purchase)
    .OrderBy(dbo.Purchase.TotalPurchaseAmount.Desc())
    .Execute();

Top can also be used in conjunction with the Distinct method.

//select the top 5 distinct persons by name
IEnumerable<dynamic> persons = db.SelectMany(
        dbo.Person.FirstName,
        dbo.Person.LastName
    )
    .Top(5)
    .Distinct()
    .From(dbo.Person)
    .OrderBy(
        dbo.Person.LastName, 
        dbo.Person.FirstName
    )
    .Execute();
Previous
In

© 2024 dbExpression. All rights reserved.