Complex Expressions

Table Of Contents

What Are Complex Expressions?

Complex Expressions are two or more Conditions grouped together to produce a single boolean result.

When you are crafting your Query instance you chain together one or more conditions.

But you might want to group some of those conditions to be evaluated together. For example the following seudo-code "select all Customers where Balance > 0 or (CreatedDate = today and (status = "new" or status = "inactive"))"

With a simple chain of And, Or we cannot achieve this meaning; this expression.

That is where Complex Expressions come into play in The Sharp Factory Framework.

Complex Expressions And Query

To start an expression add .Where() or Or() to a Query instance using the overload that takes a delegate.

The following C# code sample shows how to achieve the expression "select all Customers where Balance > 0 or (CreatedDate = today and (status = "new" or status = "inactive"))"

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

var query = new Query<CustomerProperty>()
    .BeginPredicate()
    .Where(CustomerProperty.Balance).GreaterThan(0)
    .Or((exp)=>
    {
        exp
        .Where(CustomerProperty.CreatedDate).EqualsDate(DateTime.Now)
        .Where((exp2) => 
        {
            exp2
            .Where(CustomerProperty.Status).Equals("new")
            .Or(CustomerProperty.Status).Equals("inactive");
        });
    })
    .EndPredicate();
                       
// 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

var query = new Query<CustomerProperty>()
    .Builder((builder) =>
    {
        builder
        .Predicate((builder) =>
        {
            builder
            .Where(CustomerProperty.Balance).GreaterThan(0)
            .Or((exp)=>
            {
                exp
                .Where(CustomerProperty.CreatedDate).EqualsDate(DateTime.Now)
                .Where((exp2) => 
                {
                    exp2
                    .Where(CustomerProperty.Status).Equals("new")
                    .Or(CustomerProperty.Status).Equals("inactive");
                });
            });
        });
    });
           
// 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);

The examples above are equivalent. The only difference is the syntax.