Creating predicates with fields and expressions

By default LLBLGen Pro defines several operator overloads on EntityField(2) classes to produce predicates. It also defines on the Expression class a couple of operator overloads to produce expressions quickly.

To make things easier and more fluent, a list of extension methods have been defined for IExpression and IEntityFieldCore objects to produce predicates, as well as other objects. The methods defined are:

  • Equal. To define an equality comparison predicate
  • NotEqual. To define a negated equality comparison predicate
  • GreaterThan. To define a greater than comparison predicate
  • GreaterEqual. To define a greater or equal comparison predicate
  • LesserThan. To define a lesser than comparison predicate
  • LesserEqual. To define a lesser or equal comparison predicate

The extension methods can be used directly on the expression or field:

...
.Where((OrderDetailFields.Quantity.Mul(OrderDetailFields.Price)).Equal(500))

Concatenating predicates with .And/.Or/.AndNot/.OrNot

To create longer predicates, you can use the following extension methods to create a predicate expression from two IPredicate operands: .And(predicate) / .Or(predicate) / .AndNot(predicate) / .OrNot(predicate).

Converting a predicate to boolean value

A predicate isn't a boolean value in a SQL query. QuerySpec inherits this aspect. This means that when you use a predicate as a boolean value in other areas than a Where clause (e.g. as a value in the projection or Order by), the predicate has to be converted to a boolean value.

This is done by appending the predicate with the extension method AsBooleanValue(). This method wraps the predicate with a CASE WHEN (predicate) THEN 1 ELSE 0 END statement.

Field operators: Like / StartsWith / EndsWith / Contains(string)

To produce Like predicates, the developer has a couple of options:

  • field.Like(pattern). This is the general extension method which creates a FieldLikePredicate for the field the method is called on and the pattern specified. Pattern has to contain the wild card characters.
  • field.StartsWith(pattern). This is equal to field.Like(pattern+"%");
  • field.EndsWith(pattern). This is equal to field.Like("%" + pattern);
  • field.Contains(pattern). This is equal to field.Like("%" + pattern + "%");