Show/Hide Toolbars

TMS Aurelius Documentation

Navigation: Queries

Ordering Results

Scroll Prev Top Next More

You can order the results by any property of the class being query, or by a property of an association of the class. Just use either AddOrder or OrderBy methods of the TCriteria object. You must define name of the property (or projection) being ordered, and if the order is ascending or descending. See examples below:

 

Retrieve customers ordered by Name

 

Results := Manager.Find<TCustomer>
  .Where(Linq['Name'].Like('M%'))

  .OrderBy('Name')
  .List;

 

Same query using AddOrder (instead of OrderBy):

 

Results := Manager.Find<TCustomer>
  .Where(Linq['Name'].Like('M%'))
  .AddOrder(TOrder.Asc('Name'))
  .List;

 

You can also use association aliases in orderings.

 

Retrieve all estimates which IssueDate is not null, ordered by customer name in descending order (second parameter in OrderBy specify ascending/descending - false means descending, it's true by default):

 

 

Results := Manager.Find<TEstimate>
  .CreateAlias('Customer''c')
  .Where(Linq['IssueDate'].IsNotNull)
  .OrderBy('c.Name', false)
  .List; 

 

Same query using AddOrder:

 

Results := Manager.Find<TEstimate>
  .CreateAlias('Customer''c')
  .Add(Linq['IssueDate'].IsNotNull)
  .AddOrder(TOrder.Desc('c.Name'))
  .List; 

 

If you need to order by complex expressions, it's recommended that you use a Alias projection for it. In the example below, the order refers to the EstimateSum alias, which is just an alias for the sum expression

 

Results := Manager.Find<TTC_Estimate>
  .CreateAlias('Customer''c')
  .Select(TProjections.ProjectionList
    .Add(TProjections.Sum('EstimateNo').As_('EstimateSum'))
    .Add(TProjections.Group('c.Name'))
    )
  .Where(Linq['c.Name'].Like('M%'))
  .AddOrder(TOrder.Asc('EstimateSum'))
  .ListValues;