Hi Scott. You can achieve that with FieldCompareSetPredicate. Here an example:
Statemen: I want all Customers with MOST RECENT DATE for EACH CUSTOMER-EMPLOYEE COM
// collection to fill
EntityCollection<OrdersEntity> orders = new EntityCollection<OrdersEntity>(new OrdersEntityFactory());
// general filter
IRelationPredicateBucket filter = new RelationPredicateBucket();
// -- SETUP THE SUBQUERY --
// CustomerID = OrdersMaxDate.CustomerID
IPredicate subQueryFilter1 =
new FieldCompareExpressionPredicate(
OrdersFields.CustomerId, null,
ComparisonOperator.Equal,
new Expression(OrdersFields.CustomerId.SetObjectAlias("OrdersMaxDate")) );
// EmployeeID = OrdersMaxDate.EmployeeID
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
filter.PredicateExpression.Add(
new FieldCompareSetPredicate(
OrdersFields.OrderDate, null,
OrdersFields.OrderDate.SetAggregateFunction(AggregateFunction.Max).SetObjectAlias("OrdersMaxDate"), null,
SetOperator.In,
subQueryFilters));
// data retrieval
using (DataAccessAdapter adapter = new DataAccessAdapter())
{
adapter.FetchEntityCollection(orders, filter);
}