Jan, Threre's a couple of good support threads (thanks Walaa and Otis
):
http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=6370
http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=5249
However I can't find a very clear example of your scenario, so I made one for exercise with the **Northwind **database (_SQL2K, LLBLGen v2.0, Adapter_)
Statement: **
"I want to retrieve ALL ORDERS with the MOST RECENT ORDER'S DATE of the CUSTOMER-EMPLOYEE combination".
**
Retrieval Example:
Whole data:
(OrderID, CustomerID, EmployeeID, OrderDate)
10835 ALFKI 1 1998-01-15 00:00:00.000
10952 ALFKI 1 1998-03-16 00:00:00.000
11011 ALFKI 3 1998-04-09 00:00:00.000
10692 ALFKI 4 1997-10-03 00:00:00.000
10702 ALFKI 4 1997-10-13 00:00:00.000
10643 ALFKI 6 1997-08-25 00:00:00.000
10759 ANATR 3 1997-11-28 00:00:00.000
10625 ANATR 3 1997-08-08 00:00:00.000
10926 ANATR 4 1998-03-04 00:00:00.000
10308 ANATR 7 1996-09-18 00:00:00.000
Result Statement Data:
(OrderID, CustomerID, EmployeeID, OrderDate)
10952 ALFKI 1 1998-03-16 00:00:00.000
11011 ALFKI 3 1998-04-09 00:00:00.000
10702 ALFKI 4 1997-10-13 00:00:00.000
10643 ALFKI 6 1997-08-25 00:00:00.000
10759 ANATR 3 1997-11-28 00:00:00.000
10926 ANATR 4 1998-03-04 00:00:00.000
10308 ANATR 7 1996-09-18 00:00:00.000
Possible SQL Query
SELECT * FROM orders
WHERE orders.orderDate =
( select max(orderDate)
from orders ordersMaxDate
where ordersMaxDate.customerID = orders.customerID
and ordersMaxDate.employeeID = orders.employeeID
)
Scenario's Characteristics:
1. Subquery can't be rewrite as a _JOINS _query. So you need to use FieldCompareSetPredicate.
2. Subquery filter indicate a relation.
3. Relation between Query and SubQuery do not implicate a Relation Database Object. So you can't use a IRelation object either a CustomFilter for EntityRelations.
4. In order to points 1, 2 and 3, you must to compare the query and subquery via a FieldCompareExpressionPredicate.
4. The equal (=) operator is used as the subquery must return only one value (same result as IN, however I don't know right now the performance's difference).
Code Retrieval Example
// collection to fill
EntityCollection<OrdersEntity> orders = new EntityCollection<OrdersEntity>(new OrdersEntityFactory());
// general filter
IRelationPredicateBucket filter = new RelationPredicateBucket();
// -- SETUP THE SUBQUERY --
/// CustomerID = OrdersMaxDate.CustomerID
/// Ref: Generated code - The predicate system - Adapter - FieldCompareExpressionPredicate
IPredicate subQueryFilter1 =
new FieldCompareExpressionPredicate(
OrdersFields.CustomerId, null,
ComparisonOperator.Equal,
new Expression(OrdersFields.CustomerId.SetObjectAlias("OrdersMaxDate")) );
/// EmployeeID = OrdersMaxDate.EmployeeID
/// Ref: Generated code - The predicate system - Adapter - FieldCompareExpressionPredicate
IPredicate subQueryFilter2 =
new FieldCompareExpressionPredicate(
OrdersFields.EmployeeId, null,
ComparisonOperator.Equal,
new Expression(OrdersFields.EmployeeId.SetObjectAlias("OrdersMaxDate")));
/// CustomerID == OrdersMaxDate.CustomerID and EmployeeID == OrdersMaxDate.EmployeeID
IPredicateExpression subQueryFilters = new PredicateExpression();
subQueryFilters.Add(subQueryFilter1);
subQueryFilters.AddWithAnd(subQueryFilter2);
/// OrderDate = ( MAX(OrderDate) FROM Orders ALIAS OrdersMaxDate
/// WHERE CustomerID == OrdersMaxDate.CustomerID and EmployeeID == OrdersMaxDate.EmployeeID
/// Ref: Generated code - The predicate system - Adapter - FieldCompareSetPredicate
/// Ref: Generated code - Field expressions and aggretates
filter.PredicateExpression.Add(
new FieldCompareSetPredicate(
OrdersFields.OrderDate, null,
OrdersFields.OrderDate.SetAggregateFunction(AggregateFunction.Max).SetObjectAlias("OrdersMaxDate"), null,
SetOperator.In,
subQueryFilters));
/// ... data retrieval code (depends of the LLBLGen Generator Mode)
So please adapt the concepts to your case (it's the same scenario) and let us know if you have troubles.
Hope helpful