Sorting

Table Of Contents

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 Enum

When The Sharp Factory App generates the C# code in your solution, amongst other things, it generates a collection of enumerations that represent all the properties of each entity.

The naming convention is EntityName + Property. For example an Entity called Customer will have a corresponding enum called CustomerProperty.

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 Enum as parameter.

Fluent Syntax

C#
// 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 application

var repo = new RepositoryContainer();

// create an instance of Query and 

// specify the entity's PropertyEnum

// apply sorting using

var 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 repository

var result = repo.AppDb.Sales.Customer.ToList(query);

Builder Syntax

C#
// 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 application

var repo = new RepositoryContainer();

// create an instance of Query and 

// specify the entity's PropertyEnum

// apply sorting using

var 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 repository

var 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 Enum as parameter.

The below code sample shows how to sort the results by the Customer's Last Name descending

Fluent Syntax

C#
// 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 application

var repo = new RepositoryContainer();

// create an instance of Query and 

// specify the entity's PropertyEnum

// apply sorting using

var 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 repository

var result = repo.AppDb.Sales.Customer.ToList(query);

Builder Syntax

C#
// 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 application

var repo = new RepositoryContainer();

// create an instance of Query and 

// specify the entity's PropertyEnum

// apply sorting using

var 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 repository

var result = repo.AppDb.Sales.Customer.ToList(query);