Finding Customers with Orders...

Posts   
 
    
Posts: 8
Joined: 23-Apr-2005
# Posted on: 21-May-2005 00:21:30   

I have a Customer collection, and each customer has a collection of orders.

How do I retrieve ONLY those customers with orders? I can't see how to qualify my fetch to only retrieve those customers with orders?

I have tried fetching all customers and using a prefetchpath on orders in the fetch. Then I can copy only those customers with orders to a different collection. It's just slow. There MUST be a better way (ie fetch ONLY those customers with orders)...

Thanks!

Chris

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 21-May-2005 10:47:36   

fetching only those customers with orders is not that hard simple_smile It's similar to:


select distinct customer.*
from customer inner join order on customer.customerid = order.orderid

simple_smile

or:


select *
from customer 
where customerid in
(
select customerid from order
)

On most databases, this will result in the same execution plan, so it won't matter which one you'll pick, though the first is easier to program. Though, if customer has a BLOB/text/image field, distinct can't be applied and the subquery approach is much faster.

so for the first method, pass the customer - order relation in a relationcollection (or RelationPredicateBucket if you're using adapter). For the second query, produce a FieldCompareSetPredicate in a predicateexpression simple_smile

Frans Bouma | Lead developer LLBLGen Pro