Constructing Query Conditions

Where clause, On clause, Having clause and Case clause, all of them can use the conditions constructed dynamically.

There are two ways to construct query conditions dynamically. The first way is:

BoolExpression condition = null;

if (orderDate != null)

  condition &= NW.Order.OrderDate == orderDate;

if (shipCity != String.Empty)

  condition &= NW.Order.ShipCity == shipCity;

 

SelectQuery qryOrders = OQL

  .SelectFrom(NW.Order)

  .Where(condition);

The SQL statements generated as follows (taking SQL Server as an example)

SELECT [Orders].*

FROM [Orders]

WHERE

  [Orders].[OrderDate] = '2004-1-1' AND

  [Orders].[ShipCity] = 'Macrobject City'

The second one is:

QueryCondition condition = new QueryCondition();

if (orderDate != null)

  condition = condition.And(NW.Order.OrderDate == orderDate);

if (shipCity != String.Empty)

  condition = condition.And(NW.Order.ShipCity == shipCity);

 

SelectQuery qryOrders = OQL

  .SelectFrom(NW.Order)

  .Where(condition);

The SQL statements generated as follows (taking SQL Server as an example)

SELECT [Orders].*

FROM [Orders]

WHERE

  [Orders].[OrderDate] = '2004-1-1' AND

  [Orders].[ShipCity] = 'Macrobject City'

 

Related Topics

Dynamic Construction of OQL