Syntax

Table Of Contents

The Query Class

The Query class allows you to craft from the simplest to the most complex queries.

You can then pass your instance of the Query class to one of the many available methods in the repository for the query to be applied to operations like Retrieve, Update, Delete.

It is a generic class that takes a Property Enum as a generic type parameter.

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.

Query Instance

Start by creating an instance of the Query class passing the Property Enum as generic type parameter like so:

C#
// create an instance and

// specify the entity's PropertyEnum

var query = new Query<CustomerProperty>();

Pass that instance to one of the repository methods available on that Entity's repository like this:

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

var query = new Query<CustomerProperty>();

// 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);

Anatomy of a Query

A Query is composed of multiple parts or sections.

  • Predicate: Here you specify all the where clauses.

  • Sorting: Here you specify all the sorting criteria.

  • Navigation Properties: Here you specify all the related entities to include in the result.

  • Pagination: Here you specify the pagination criteria to apply to the query.

The different parts of the query are optional.

Fluent Syntax

The following example demonstrates how to build the query "skeleton" using Fluent Syntax.

This means concatenating conditions in a single statement.

C#
// create an instance and

// specify the entity's PropertyEnum

// specify the entity's NavPropertyEnum to make the NavProperties available.

var query = new Query<CustomerProperty, CustomerNavProperty>()   
    .BeginPredicate()
        // where conditions go here

    .EndPredicate()
    
    .BeginSorting()
        // sorting criteria goes here

    .EndSorting()

    .BeginNavProps()
        // navigation properties go here

    .EndNavProps()

    .BeginPagination()
        // sorting criteria goes here

    .EndPagination();

Builder Syntax

The following example demonstrates how to build the query "skeleton" using Builder Syntax.

This means concatenating conditions using delegates.

Builder syntax can make it easier to dynamically craft queries for more complex scenarios.

C#
// create an instance and

// specify the entity's PropertyEnum

// specify the entity's NavPropertyEnum to make the NavProperties available.

var query = new Query<CustomerProperty, CustomerNavProperty>()  
    .Builder(builder => 
    {
        builder
        .Predicate((predicate) =>
        {
            // where conditions go here

        })
        .NavProps((navProps) => 
        {
            // navigation properties go here

        })
        .Sorting((sort) => 
        {
            // sorting criteria goes here

        })
        .Pagination((pagination) => 
        {
            // sorting criteria goes here

        });
    });

Builder Syntax vs Fluent Syntax

They both produce equivalent SQL Queries.

Choosing one or the other is a matter of taste.

We recommend you experiment with both and find the best option for your use case.

For more information on how to craft queries and use filtering see the Filtering section of the documentation.