Sorting
Sort ascending or descending on one or more properties.
On this page
Sorting The Query#
Sorting can be used to build your Query instance.
To learn about how to craft the Query instance go to the Filtering section of the documentation.
You can also check out the Query Syntax section of the documentation.
Property Container#
When The Sharp Factory App generates the C# code in your solution, amongst other things, it generates a property container class for each entity, with one static field per column. Each field has the column's .NET type.
The naming convention is EntityName + Property. For example an Entity called Customer will have a corresponding class called CustomerProperty, and CustomerProperty.LastName is a Prop<CustomerProperty, string>.
The below code sample shows how to sort the results by the Customer's Last Name ascending.
Sort Ascending#
To sort the results of your query in ascending order you can call the OrderBy method on the Query class.
This method takes a property of the entity's property container, for example CustomerProperty.LastName.
Fluent Syntax#
// create an instance of the Repository Container// remember this can be a singleton if you// declare it early on and reuse it for the// lifetime of your applicationvar repo = new RepositoryContainer();// create an instance of Query and// specify the entity's property container// apply sortingvar query = new Query<CustomerProperty>() .BeginSorting() .OrderBy(CustomerProperty.LastName) .EndSorting();// pass the query to one of the repository methods// in this case the ToList on the Customer repositoryvar result = repo.AppDb.Sales.Customer.ToList(query);Builder Syntax#
// create an instance of the Repository Container// remember this can be a singleton if you// declare it early on and reuse it for the// lifetime of your applicationvar repo = new RepositoryContainer();// create an instance of Query and// specify the entity's property container// apply sortingvar query = new Query<CustomerProperty>() .Builder(builder => { builder .Sorting((sort) => { sort .OrderBy(CustomerProperty.LastName); }); });// pass the query to one of the repository methods// in this case the ToList on the Customer repositoryvar result = repo.AppDb.Sales.Customer.ToList(query);Sort Descending#
To sort the results of your query in descending order you can call the OrderByDescending method on the Query class.
This method takes a property of the entity's property container, for example CustomerProperty.LastName.
The below code sample shows how to sort the results by the Customer's Last Name descending
Fluent Syntax#
// create an instance of the Repository Container// remember this can be a singleton if you// declare it early on and reuse it for the// lifetime of your applicationvar repo = new RepositoryContainer();// create an instance of Query and// specify the entity's property container// apply sortingvar query = new Query<CustomerProperty>() .BeginSorting() .OrderByDescending(CustomerProperty.LastName) .EndSorting();// pass the query to one of the repository methods// in this case the ToList on the Customer repositoryvar result = repo.AppDb.Sales.Customer.ToList(query);Builder Syntax#
// create an instance of the Repository Container// remember this can be a singleton if you// declare it early on and reuse it for the// lifetime of your applicationvar repo = new RepositoryContainer();// create an instance of Query and// specify the entity's property container// apply sortingvar query = new Query<CustomerProperty>() .Builder(builder => { builder .Sorting((sort) => { sort .OrderByDescending(CustomerProperty.LastName); }); });// pass the query to one of the repository methods// in this case the ToList on the Customer repositoryvar result = repo.AppDb.Sales.Customer.ToList(query);Sort By Several Columns#
Call OrderBy and OrderByDescending as many times as needed: the results are sorted by the first one, then by the next, in the order of the calls.
The below code sample shows how to sort the results by the Customer's Last Name ascending, then by the Created Date descending.
Fluent Syntax#
// create an instance of the Repository Container// remember this can be a singleton if you// declare it early on and reuse it for the// lifetime of your applicationvar repo = new RepositoryContainer();// create an instance of Query and// specify the entity's property container// apply sortingvar query = new Query<CustomerProperty>() .BeginSorting() .OrderBy(CustomerProperty.LastName) .OrderByDescending(CustomerProperty.CreatedDate) .EndSorting();var result = repo.AppDb.Sales.Customer.ToList(query);Builder Syntax#
// create an instance of the Repository Container// remember this can be a singleton if you// declare it early on and reuse it for the// lifetime of your applicationvar repo = new RepositoryContainer();// create an instance of Query and// specify the entity's property container// apply sortingvar query = new Query<CustomerProperty>() .Builder(builder => { builder .Sorting((sort) => { sort .OrderBy(CustomerProperty.LastName) .OrderByDescending(CustomerProperty.CreatedDate); }); });var result = repo.AppDb.Sales.Customer.ToList(query);