Filtering
Filter with conditions joined by And and Or, in fluent or builder syntax.
On this page
Query Instance#
Start by creating an instance of the Query class passing the entity's property container as generic type parameter like so:
// create an instance and// specify the entity's property containervar query = new Query<CustomerProperty>();Pass that instance to one of the repository methods available on that Entity's repository like this:
// 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 containervar query = new Query<CustomerProperty>();// 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);Examples#
For example if we wanted to say "All customers where the balance is greater than zero":
Fluent Syntax#
// create an instance and// specify the entity's property containervar query = new Query<CustomerProperty>() .BeginPredicate() .Where(CustomerProperty.Balance).GreaterThan(0) .EndPredicate();Builder Syntax#
// create an instance and// specify the entity's property containervar query = new Query<CustomerProperty>() .Builder(builder => { builder .Predicate((predicate) => { predicate.Where(CustomerProperty.Balance).GreaterThan(0); }); });And Or Logical Operators#
You can further craft your query by appending more conditions using the Where( means And ) or the Or operators.
Let's build on our previous example. "All customers where the balance is greater than 1000 or less than 500".
Fluent Syntax#
// create an instance and// specify the entity's property containervar query = new Query<CustomerProperty>() .BeginPredicate() .Where(CustomerProperty.Balance).GreaterThan(1000) .Or(CustomerProperty.Balance).LessThan(500) .EndPredicate();Builder Syntax#
// create an instance and// specify the entity's property containervar query = new Query<CustomerProperty>() .Builder(builder => { builder .Predicate((predicate) => { predicate .Where(CustomerProperty.Balance).GreaterThan(1000) .Or(CustomerProperty.Balance).LessThan(500); }); });The Or operator works in the same way that the And(Where) operator. Just replace the word Where for Or.
Typed Properties#
Each field of the property container has its column's .NET type: CustomerProperty.Balance is a Prop<CustomerProperty, decimal>. The compiler checks every value you pass against that type, and offers only the conditions the type has.
// the values have the columns' typesvar query = new Query<CustomerProperty>() .BeginPredicate() .Where(CustomerProperty.Balance).GreaterThan(1000m) .Or(CustomerProperty.LastName).StartsWith("Sm") .Where(CustomerProperty.ClosedOn).IsNull() .EndPredicate() .BeginSorting() .OrderBy(CustomerProperty.LastName) .EndSorting();A value of the wrong type, or a condition the column's type does not have, does not compile:
// Balance is a decimal: a string does not compile.Where(CustomerProperty.Balance).Equals("1000")// StartsWith is for text columns only.Where(CustomerProperty.Balance).StartsWith("1")// IsNull is for columns that allow null only.Where(CustomerProperty.Id).IsNull()The same properties are used in Where, Or, OrderBy, OrderByDescending and Set (see Update).
Supported Conditions#
For a comprehensive list of supported conditions please read the Conditions documentation.
Complex Expressions#
In some circumstances you might want to group conditions into more elaborate expressions in which these conditions might be evaluated together.
For more information on how to craft these types of queries please go to the Complex Expressions section of the documentation.