SQL | Predicate class to use |
Field BETWEEN 3 AND 5 Field BETWEEN field2 AND 4 |
FieldBetweenPredicate |
Field = Field2 Field < (Field2 * 4) |
FieldCompareExpressionPredicate |
Field Is NULL | FieldCompareNullPredicate |
Field IN (1, 2, 3, 5) | FieldCompareRangePredicate |
Field IN ( SELECT Field FROM Foo WHERE ...) |
FieldCompareSetPredicate |
Field = 3 Field != "Foo" |
FieldCompareValuePredicate |
Field LIKE "Foo%" | FieldLikePredicate |
// C# IEntityField2 field = EntityFieldFactory.Create(OrderFieldIndex.OrderDate); EntityField2 field = OrderFields.OrderDate; IEntityField2 field = myOrder.Fields[(int)OrderFieldIndex.OrderDate];
' VB.NET Dim field As IEntityField2 = EntityFieldFactory.Create(OrderFieldIndex.OrderDate) Dim field as EntityField2 = OrderFields.OrderDate Dim field As IEntityField2 = myOrder.Fields(CInt(OrderFieldIndex.OrderDate))
![]() |
The feature discussed in this paragraph is not available for VB.NET 2002/2003 users, because VB.NET 2002/2003 doesn't support operator overloading, and users of VB.NET 2002/2003 have to fall back on the more verbose way to constructing filters. VB.NET 2005 supports operator overloading in full and thus also this feature. C# 2002/2003 supports operator overloading. |
// C# IPredicateExpression B = ((Table1Fields.Foo == "One") & (Table1Fields.Bar == "Two")) // A | (Table2Fields.Bar2 == "Three");
' VB.NET 2005 Dim B IPredicateExpression = ((Table1Fields.Foo = "One") And (Table1Fields.Bar = "Two")) _ ' A Or (Table2Fields.Bar2 = "Three")
// C# IPredicateExpression B = ((Table1Fields.Foo == "One") & (Table1Fields.Bar == "Two")) // A | !(Table2Fields.Bar2 == "Three");
' VB.NET 2005 Dim B IPredicateExpression = ((Table1Fields.Foo = "One") And (Table1Fields.Bar = "Two")) _ ' A Or Not (Table2Fields.Bar2 = "Three")
... WHERE TableFields.Foo = "One" OR TableFields.Foo = "Two" OR TableFields.Foo = "Three" OR TableFIelds.Foo = "Four"
// C# IPredicateExpression filter = ((TableFields.Foo=="One") | (TableFields.Foo=="Two")) .AddWithOr(TableFields.Foo=="Three") .AddWithOr(TableFields.Foo=="Four); // which is equal to: IPredicateExpression filter = new PredicateExpression(); filter.Add(TableFields.Foo=="One") .AddWithOr(TableFields.Foo=="Two") .AddWithOr(TableFields.Foo=="Three") .AddWithOr(TableFields.Foo=="Four); // which is equal to: IPredicateExpression filter = new PredicateExpression(); filter.Add(new FieldCompareValuePredicate(TableFields.Foo, ComparisonOperator.Equal, "One")) .AddWithOr(new FieldCompareValuePredicate(TableFields.Foo, null, ComparisonOperator.Equal, "Two")) .AddWithOr(new FieldCompareValuePredicate(TableFields.Foo, null, ComparisonOperator.Equal, "Three")) .AddWithOr(new FieldCompareValuePredicate(TableFields.Foo, null, ComparisonOperator.Equal, "Four"));
' VB.NET 2005 Dim filter As IPredicateExpression = _ ((TableFields.Foo="One") Or (TableFields.Foo="Two")).AddWithOr(TableFields.Foo="Three").AddWithOr(TableFields.Foo="Four) ' which is equal to: (VB.NET 2005) Dim filter As New PredicateExpression() filter.Add(TableFields.Foo="One").AddWithOr(TableFields.Foo="Two").AddWithOr(TableFields.Foo="Three").AddWithOr(TableFields.Foo="Four) ' which is equal to: (VB.NET 2002/2003/2005) Dim filter As New PredicateExpression() ' NOTE: the following line is specified on multiple lines here for readability, VB.NET wants you to mention the statements on 1 line filter.Add(New FieldCompareValuePredicate(TableFields.Foo, Nothing, ComparisonOperator.Equal, "One")) _ .AddWithOr(New FieldCompareValuePredicate(TableFields.Foo, Nothing, ComparisonOperator.Equal, "Two")) _ .AddWithOr(New FieldCompareValuePredicate(TableFields.Foo, Nothing, ComparisonOperator.Equal, "Three")) _ .AddWithOr(New FieldCompareValuePredicate(TableFields.Foo, Nothing, ComparisonOperator.Equal, "Four"))
C# | VB.NET 2005 | Descr. | Example | Object produced |
+ | + | Addition | (OrderDetailsFields.Quantity + 10) | Expression |
| | Or | SortClause construction | (CustomerFields.CompanyName | SortOperator.Ascending) | SortClause |
/ | / | Division | (OrderDetailsFields.Quantity / 10) | Expression |
== | = | Equality | (CustomerFields.CompanyName == "Foo Inc.") | FieldCompareValuePredicate, FieldCompareExpressionPredicate, FieldCompareRangePredicate, FieldCompareNullPredicate |
> | > | Greater Than | (OrderDetailsFields.Quantity > 10) | FieldCompareValuePredicate, FieldCompareExpressionPredicate |
>= | >= | Greater Than Or Equal | (OrderDetailsFields.Quantity >= 10) | FieldCompareValuePredicate, FieldCompareExpressionPredicate |
!= | <> | Equality | (CustomerFields.CompanyName != "Foo Inc.") | FieldCompareValuePredicate, FieldCompareExpressionPredicate, FieldCompareRangePredicate, FieldCompareNullPredicate |
< | < | Lesser Than | (OrderDetailsFields.Quantity < 10) | FieldCompareValuePredicate, FieldCompareExpressionPredicate |
<= | <= | Lesser Than Or Equal | (OrderDetailsFields.Quantity <= 10) | FieldCompareValuePredicate, FieldCompareExpressionPredicate |
% | Mod | Like creation | (CustomerFields.CompanyName % "Foo%") | FieldLikePredicate |
* | * | Multiplication | (OrderDetailsFields.Quantity * 10) | Expression |
- | - | Substraction | (OrderDetailsFields.Quantity - 10) | Expression |
// C# PredicateExpression filter = (OrderFields.CustomerID == "CHOPS") & (new FieldBetweenPredicate(OrderFields.ShippedDate, dateStart, dateEnd));
' VB.NET Dim filter As PredicateExpression = (OrderFields.CustomerID = "CHOPS") And _ (New FieldBetweenPredicate(OrderFields.ShippedDate, dateStart, dateEnd))
![]() |
The SQL examples given can contain absolute values. The SQL generated by the predicates will never contain absolute values as absolute values will be converted to parameters. |
Description | compares the entity field specified using a BETWEEN operator and results in true if the field's value is greater than or equal to the value of valueBegin and less than or equal to the value of valueEnd. valueBegin and valueEnd can be any value or an EntityField2, as shown in the examples. |
SQL equivalent |
Field BETWEEN valueStart AND valueEnd Field BETWEEN OtherField AND valueEnd Field BETWEEN valueStart AND OtherField |
Operators | none. |
Example |
// C# filter.Add(new FieldBetweenPredicate( OrderFields.OrderDate, null, dateStart, dateEnd)); // or: ShippingDate BETWEEN RequiredDate and dateEnd filter.Add(new FieldBetweenPredicate( OrderFields.ShippedDate, null, OrderFields.RequiredDate, dateEnd)); ' VB.NET filter.Add(new FieldBetweenPredicate( OrderFields.OrderDate, Nothing, dateStart, dateEnd)) ' or: ShippingDate BETWEEN RequiredDate and dateEnd filter.Add(new FieldBetweenPredicate( OrderFields.ShippedDate, Nothing, OrderFields.RequiredDate, dateEnd)) |
Can be used for in-memory filtering | Yes |
Description | compares the entity field specified with the expression specified, using the ComparisonOperator specified. See for a detailed description about expressions the Field expressions and aggregates section |
SQL equivalent examples | Field > (OtherField * 2) Field <= OtherField |
Operators | All ComparisonOperator operators: Equal, GreaterEqual, GreaterThan, LessEqual, LesserThan, NotEqual |
Example | This example creates a predicate which compares Order.OrderDate with Order.ShippingDate. The example might look a little verbose, but Expression objects are
re-usable, which allows you to define the Expression objects once and re-use them each time you need them with a predicate class for example. The example illustrates
a very basic expression, but the expression you can specify can be very complex. See for more information about expressions
Field expressions and aggregates
// C# bucket.PredicateExpression.Add(new FieldCompareExpressionPredicate( OrderFields.OrderDate, null, ComparisonOperator.Equal, new Expression(OrderFields.ShippedDate))); // which is equal to: bucket.PredicateExpression.Add((OrderFields.OrderDate == OrderFields.ShippedDate)); ' VB.NET bucket.PredicateExpression.Add(New FieldCompareExpressionPredicate( _ OrderFields.OrderDate, Nothing, ComparisonOperator.Equal, _ New Expression(OrderFields.ShippedDate))) ' which is equal to: (VB.NET 2005) bucket.PredicateExpression.Add((OrderFields.OrderDate = OrderFields.ShippedDate)) Example which filters on orders which have been shipped 4 days after the orderdate:
// C# bucket.PredicateExpression.Add(new FieldCompareExpressionPredicate( OrderFields.ShippedDate, null, ComparisonOperator.Equal, new Expression(OrderFields.OrderDate, ExOp.Add, 4))); // which is equal to: bucket.PredicateExpression.Add((OrderFields.ShippedDate == (OrderFields.OrderDate + 4))); ' VB.NET bucket.PredicateExpression.Add(New FieldCompareExpressionPredicate( _ OrderFields.ShippedDate, Nothing, ComparisonOperator.Equal, _ New Expression(OrderFields.OrderDate, ExOp.Add, 4))) ' which is equal to: (VB.NET 2005) bucket.PredicateExpression.Add((OrderFields.ShippedDate == (OrderFields.OrderDate + 4))) |
Can be used for in-memory filtering | Yes |
Description | compares the entity field specified with NULL. |
SQL equivalent | Field IS NULL |
Operators | none. |
Example |
// C# bucket.PredicateExpression.Add(new FieldCompareNullPredicate(OrderFields.OrderDate, null)); // which is equal to: bucket.PredicateExpression.Add((OrderFields.OrderDate==System.DBNull.Value)); ' VB.NET bucket.PredicateExpression.Add(New FieldCompareNullPredicate(OrderFields.OrderDate, Nothing)) ' which is equal to: (VB.NET 2005) bucket.PredicateExpression.Add((OrderFields.OrderDate = System.DBNull.Value)) |
Can be used for in-memory filtering | Yes |
Description | compares the entity field specified with the range of specified values using the IN operator. The range is not a subquery, use FieldCompareSetPredicate for that. The range can be supplied in an ArrayList, in an array or hardcoded in the predicate constructor. |
SQL equivalent examples | Field IN (1, 2, 5, 10) Field IN ("Foo", "Bar", "Blah") |
Operators | none. |
Example | This example creates a predicate which compares Order.EmployeeId with the range 1, 2, 5, stored in an array. The values can also be specified directly
in the constructor.
// C# int[] values = new int[3] {1, 2, 5}; bucket.PredicateExpression.Add(new FieldCompareRangePredicate( OrderFields.EmployeeId, null, values)); // which is equal to: bucket.PredicateExpression.Add(OrderFields.EmployeeId == values); ' VB.NET Dim values As Integer() = New Integer(2) {1, 2, 5} bucket.PredicateExpression.Add(New FieldCompareRangePredicate( _ OrderFields.EmployeeId, Nothing, values)) ' which is equal to: bucket.PredicateExpression.Add(OrderFields.EmployeeId = values) |
Can be used for in-memory filtering | Yes |
Description | compares the entity field specified with the set of values defined by the subquery elements, using the SetOperator specified. The FieldCompareSetPredicate
is the predicate you'd like to use when you want to compare a field's value with a range of values retrieved from another table / view (or the same table / view)
using a subquery.
FieldCompareSetPredicates also allows you to define EXISTS () queries. It is then not necessary to specify an IEntityField2 object with the predicate's constructor (specify null / nothing) as it is ignored when building the SQL. Keep in mind that EXISTS() queries are semantically the same as IN queries and IN queries are often simpler to formulate. The FieldCompareSetPredicate supports advanced comparison operators like ANY, ALL and combinations of these with comparison operators like Equal (=) or GreaterThan (>). If the set is just 1 value in size (because you've specified a limit on the number of rows to return), it's wise to use the Equal operator instead of the IN operator as most databases will be rather slow with IN and just 1 value compared to the Equal operator. |
SQL equivalent examples | Field IN (SELECT OtherField FROM OtherTable WHERE Foo=2) EXISTS (SELECT * FROM OtherTable) |
Operators | All SetOperator operators: In, Exists, Equal, EqualAny, EqualAll, LessEqual, LessEqualAny, LessEqualAll, LesserThan, LesserThanAny, LesserThanAll, GreaterEqual, GreaterEqualAny, GreaterEqualAll, GreaterThan, GreaterThanAny, GreaterThanAll, NotEqual, NotEqualAny, NotEqualAll |
Example | This example illustrates the query: Customer.CustomerID IN (SELECT CustomerID FROM Orders WHERE Employee=2)
// C# bucket.PredicateExpression.Add(new FieldCompareSetPredicate( CustomerFields.CustomerID, null, OrderFields.CustomerID, null, SetOperator.In, (OrderFields.EmployeeID == 2))); ' VB.NET bucket.PredicateExpression.Add(New FieldCompareSetPredicate( _ CustomerFields.CustomerID, Nothing, OrderFields.CustomerID, Nothing, _ SetOperator.In, _ New FieldCompareValuePredicate(OrderFieldIndex.EmployeeID, _ Nothing, ComparisonOperator.Equal, 2))) ' which is equal to bucket.PredicateExpression.Add(new FieldCompareSetPredicate( _ CustomerFields.CustomerID, Nothing, OrderFields.CustomerID, Nothing, _ SetOperator.In, (OrderFields.EmployeeID = 2))) |
Can be used for in-memory filtering | No |
Description | compares the entity field specified with the value specified, using the ComparisonOperator specified. If the value to compare is a string, you will get a case sensitive compare if the database is using a case sensitive collation (like Oracle). You can perform case insensitive compares however, by setting the CaseSensitiveCollation property to true prior to passing the predicate to a fetch method like FetchEntityCollection(). This will perform the UPPERCASE variant of the field with the pattern specified. Please note that if you've set CaseSensitiveCollaction to true, you've to specify your pattern in uppercase as well. | ||
SQL equivalent examples | Field > 3 Field = "Foo" |
||
Operators | All ComparisonOperator operators: Equal, GreaterEqual, GreaterThan, LessEqual, LesserThan, NotEqual | ||
Example | This example creates a predicate which compares Order.EmployeeID with the value 2
// C# bucket.PredicateExpression.Add(new FieldCompareValuePredicate( OrderFields.EmployeeID, null, ComparisonOperator.Equal, 2)); // which is equal to: bucket.PredicateExpression.Add(OrderFields.EmployeeID == 2); ' VB.NET bucket.PredicateExpression.Add(New FieldCompareValuePredicate( _ OrderFields.EmployeeID, Nothing, ComparisonOperator.Equal, 2)) ' which is equal to: bucket.PredicateExpression.Add(OrderFields.EmployeeID = 2) |
||
Can be used for in-memory filtering | Yes
|
Description | SqlServer specific. Compares the entity field specified with the pattern specified using the FullTextSearch operator specified. |
SQL equivalent | CONTAINS(Field, "Bla") FREETEXT(Field, "Bla") |
Operators | All FullTextSearchOperator operators: Contains, Freetext |
Example |
// C# bucket.PredicateExpression.Add(new FieldFullTextSearchPredicate( CustomerFields.CompanyName, null, FullTextSearchOperator.Contains, "Solution")); ' VB.NET bucket.PredicateExpression.Add(new FieldFullTextSearchPredicate( _ CustomerFields.CompanyName, Nothing, _ FullTextSearchOperator.Contains, "Solution")) The next example shows a filter on two fields, using the SqlServer 2005 specific feature to accept multiple fields for the same operator.
// C# bucket.PredicateExpression.Add(new FieldFullTextSearchPredicate( new IEntityField2[] { CustomerFields.CompanyName, CustomerFields.ContactName}, FullTextSearchOperator.Contains, "Solution")); ' VB.NET bucket.PredicateExpression.Add(New FieldFullTextSearchPredicate( _ New IEntityField2() { CustomerFields.CompanyName, CustomerFields.ContactName }, _ FullTextSearchOperator.Contains, "Solution")) |
Can be used for in-memory filtering | No. Use FieldLikePredicate for in-memory full-text searches, using a regular expression. |
Description | compares the entity field specified with the pattern specified, using the LIKE operator. The pattern should contain the wildcard, which is '%' (also for MS Access). FieldLikePredicate performs a LIKE compare using the case sensitivity setting of the database system the query is executed on: the SQL generated does not contain any collation information nor any case insensitive setting if the database is using case sensitive comparison operations by default (Oracle, some SqlServer installations). You can perform case insensitive compares however, if the database is case sensitive, by setting the CaseSensitiveCollation property to true prior to passing the predicate to a fetch method like FetchEntityCollection(). This will perform the UPPERCASE variant of the field with the pattern specified.Please note that if you've set CaseSensitiveCollaction to true, you've to specify your pattern in uppercase as well. |
SQL equivalent examples | Field LIKE '%bla' Field LIKE 'bla%' |
Operators | none. |
Example | This example creates a predicate which compares Customer.CompanyName to the pattern "Solution%".
// C# filter.Add(new FieldLikePredicate(CustomerFields.CompanyName, null, "Solution%")); // Which is equal to: filter.Add(CustomerFields.CompanyName % "Solution%"); ' VB.NET filter.Add(New FieldLikePredicate(CustomerFields.CompanyName, Nothing, "Solution%")) ' Which is equal to: (VB.NET 2005) filter.Add(CustomerFields.CompanyName Mod "Solution%") Note, that the operator syntaxis is a little odd in VB.NET, due to the fact that there isn't an ability to add new operators to VB.NET/C#. |
Can be used for in-memory filtering | Yes. When used in in-memory filters, the pattern can either be a normal LIKE statement pattern with '%' wildcards, or it can be a full regular expression. If the pattern is a regular expression, be sure to set the property PatternIsRegEx to true. See also the LLBLGen Pro reference manual on more detailed information about the properties of the FieldLikePredicate |
Description | Predicate class which performs an aggregate function on a set of entities and returns true or false depending if that aggregated value matches a specified expression. The set of entities this predicate is applied on, for example a collection of OrderEntity instances, are the elements of a member property with the name specified which match the specified filter, for example 'Orders' in a set of CustomerEntity instances. |
Operators | All ComparisonOperator operators: Equal, GreaterEqual, GreaterThan, LessEqual, LesserThan, NotEqual |
Example | This example filters a set of customers to find all customers with at least 10 orders. customers is a collection of CustomerEntity instances.
// C# IPredicate filter = new AggregateSetPredicate( CustomerEntity.MemberNames.Orders, // member to apply the aggregate on AggregateSetFunction.Count, // aggregate function to apply. OrderFields.OrderId, // value producer. Can be a field as well. ComparisonOperator.GreaterThan, // comparison operator for the aggregateset 10, // value to compare with null); // additional filter to apply to the set. IEntityView2 filteredCustomers = new EntityView2<CustomerEntity>(customers, filter); ' VB.NET Dim filter As IPredicate = New AggregateSetPredicate( _ CustomerEntity.MemberNames.Orders, _ ' member to apply the aggregate on AggregateSetFunction.Count, _ ' aggregate function to apply. OrderFields.OrderId, _ ' value producer. Can be a field as well. ComparisonOperator.GreaterThan, _ ' comparison operator for the aggregateset 10, _ ' value to compare with Nothing) ' additional filter to apply to the set. Dim filteredCustomers As IEntityView2 = New EntityView2(Of CustomerEntity)(customers, filter) |
Description | Predicate class to filter in-memory entity collections based on a specified callback function. Use this predicate to filter entities based on logic which is best expressed in a normal .NET language, like C# or VB.NET. The generic variant accepts a Predicate(Of T) (.NET 2.0+) or Lambda expression (.NET 3.5) which is then used as a filter. |
Operators | None |
Example | This example filters a set of customers which have a CompanyName with length of 20 or higher. customers is a collection of CustomerEntity instances.
// C# // Create an entity view from customers based on the DelegatePredicate filter. // Uses an anonymous method, which is a .NET 2.0 feature. IEntityView2 customersWithLongName = new EntityView2<CustomerEntity>(customers, new DelegatePredicate( delegate(IEntityCore toExamine) { return ((CustomerEntity)toExamine).CompanyName.Length > 20; })); ' VB.NET ' VB.NET doesn't support anonymous methods, so we need a separate method. Public Function CompareCustomerCompanyName(ByVal toExamine As CustomerEntity) As Boolean Return toExamine.CompanyName.Length > 20 End Function For a .NET 3.5 example, please see the EntityView2 filtering example. |
Description | Predicate class which allows in-memory filters to perform a predicate (filter) on one or more related entities. The entity this predicate is applied on has to have a member property with the name specified. Each element in that member (or the member itself, in case of a single instance) will be interpreted with the specified filter. The result of that interpretation is used together with the MemberOperator specified what the result of this predicate will be: true or false, in which case the entity this predicate is applied on is accepted (true) or not (false). |
Operators | All MemberOperator operators: All, Any or Null (no data) |
Example | This example shows in memory filtering of customers which filters all customers which have orders with a total > 5000. The order total is calculated by SUMming the orderdetail total values. The order detail total is (UnitPrice * quantity) - ((UnitPrice * quantity)* discount). customers is a collection of CustomerEntity instances.
// C# IPredicate filter = new MemberPredicate( CustomerEntity.MemberNames.Orders, // the member to apply the specified filter on. MemberOperator.Any, // operator for applying contained filter. new AggregateSetPredicate( // aggregate predicate to apply on a member OrderEntity.MemberNames.OrderDetails, // member to apply the aggregate on AggregateSetFunction.Sum, // aggregate function to apply. ((OrderDetailsFields.UnitPrice * OrderDetailsFields.Quantity)- ((OrderDetailsFields.UnitPrice * OrderDetailsFields.Quantity) * OrderDetailsFields.Discount)), // value producer expression. ComparisonOperator.GreaterThan, // comparison operator for the aggregateset 5000.0M, // value to compare with null // filter which specifies the set to apply the aggregate on. ) ); IEntityView2 filteredCustomers = new EntityView2<CustomerEntity>(customers, filter); ' VB.NET Dim filter As IPredicate = New MemberPredicate( _ CustomerEntity.MemberNames.Orders, _ ' the member to apply the specified filter on. MemberOperator.Any, _ ' operator for applying contained filter. New AggregateSetPredicate( _ ' aggregate predicate to apply on a member OrderEntity.MemberNames.OrderDetails, _ ' member to apply the aggregate on AggregateSetFunction.Sum, _ ' aggregate function to apply. ((OrderDetailsFields.UnitPrice * OrderDetailsFields.Quantity)- _ ((OrderDetailsFields.UnitPrice * OrderDetailsFields.Quantity) _ * OrderDetailsFields.Discount)), _ ' value producer expression. ComparisonOperator.GreaterThan, _ ' comparison operator for the aggregateset 5000.0D, _ ' value to compare with Nothing _ ' filter which specifies the set to apply the aggregate on. )) Dim filteredCustomers As new EntityView2(Of CustomerEntity)(customers, filter) |