Seth wrote:
Is it possible to generate the following kind of query:
SELECT JobId
FROM Job
WHERE (SELECT SUM(Billing.Amount) FROM Billing WHERE Job.JobId = Billing.JobId) > 0
AND
(SELECT COUNT(Payment.PaymentId) FROM Payment WHERE Job.JobId = Payment.JobId) = 0
I think your SQL could be rewritten for less complexity:
SELECT
Job.JobId
FROM
Job
inner join Billing on Job.JobId = Billing.JobId
inner join Payment on Job.JobId = Payment.JobId
GROUP BY
Job.JobId
HAVING
SUM(Billing.Amount) > 0
and COUNT(Payment.PaymentId) = 0
Then, you can use _FieldCompareSetPredicate _in your collection filter.
Look at this simple example:
Statement: I want all orders related to customers that have more than 20 orders.
Possible SQL:
select * from orders
where customerId in
(
SELECT c.customerId
FROM customers c
inner join orders o on c.customerId = o.customerid
GROUP BY c.customerId
HAVING count(o.orderid) > 20
)
**
Possible LLBLGen code (2.0, Adapter)**
// sub-set relations
IRelationCollection subSetRelations = new RelationCollection();
subSetRelations.Add(CustomersEntity.Relations.OrdersEntityUsingCustomerId);
// sub-set groupBy section
IGroupByCollection subSetGroupByClause = new GroupByCollection();
subSetGroupByClause.Add(CustomersFields.CustomerId);
subSetGroupByClause.HavingClause = new PredicateExpression(OrdersFields.OrderId.SetAggregateFunction(AggregateFunction.Count) > 20);
// collection to fetch
EntityCollection<OrdersEntity> orders = new EntityCollection<OrdersEntity>(new OrdersEntityFactory());
// collection filter
IRelationPredicateBucket ordersFilter = new RelationPredicateBucket();
ordersFilter.PredicateExpression.Add(
new FieldCompareSetPredicate(
OrdersFields.CustomerId, null,
CustomersFields.CustomerId, null,
SetOperator.In,
null,
bucket.Relations,"", 0, null, false,
subSetGroupByClause));
// fetch results
using (DataAccessAdapter adapter = new DataAccessAdapter())
{
adapter.FetchEntityCollection(orders, ordersFilter);
}
// show results
foreach (OrdersEntity order in orders )
{
Console.WriteLine(order.OrderId);
}
So in your case you need to add two FieldCompareSetPredicate's to the collection filter
Hope helpful