Prefetching 3 node with filters...

Posts   
 
    
TPS
User
Posts: 1
Joined: 20-Nov-2009
# Posted on: 20-Nov-2009 18:51:18   

Hi,

I'm trying to prefecth 3 levels in one and filter the 3rd node.

My relation are Order > Order Detail > Voucher and I want to get all orders that contain a voucher with an specific voucherCode and filter by orderNumber.

The problem is that I'm getting all the orders with that orderNumber without regardless the voucherCode, here is my code:


IPredicateExpression predicateExpression = new PredicateExpression(OrdersFields.OrderId == orderNumberCriteria);

IPredicateExpression voucherPredicateExpression = new PredicateExpression(VoucherFields.VoucherCode % voucherCodeCriteria);

IRelationCollection voucherRelations = new RelationCollection();
voucherRelations.Add(OrderDetailEntity.Relations.VoucherEntityUsingOrderDetailId);

IPrefetchPath prefetchPath = new PrefetchPath(EntityType.OrdersEntity);
prefetchPath.Add(OrdersEntity.PrefetchPathOrderDetail)
                      .SubPath.Add(OrderDetailEntity.PrefetchPathVoucher, 0, voucherPredicateExpression, voucherRelations);

OrdersCollection ordersCollection = new OrdersCollection();
            ordersCollection.GetMulti(predicateExpression, prefetchPath);

What I'm doing wrong?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 20-Nov-2009 20:27:29   

The problem is that you are passing a filter in the prefetchpath, that will filter the results for that node only. If you want to filter by that field in the whole graph you have to include such filter in the main filter. Your code should look like this:

// the prefetchpath
IPrefetchPath prefetchPath = new PrefetchPath(EntityType.OrdersEntity);
prefetchPath.Add(OrdersEntity.PrefetchPathOrderDetail)
                     .SubPath.Add(OrderDetailEntity.PrefetchPathVoucher);

// prepare the filter
IPredicateExpression predicateExpression = new PredicateExpression(OrdersFields.OrderId == orderNumberCriteria);
predicateExpression.Add(VoucherFields.VoucherCode % voucherCodeCriteria);

// the relations so the filter would be valid
IRelationCollection orderRelations = new RelationCollection();
orderRelations.Add(OrderEntity.Relations.OrderDetailEntityUsingOrderId);
orderRelations.Add(OrderDetailEntity.Relations.VoucherEntityUsingOrderDetailId);

// fetch
OrdersCollection ordersCollection = new OrdersCollection();
            ordersCollection.GetMulti(predicateExpression, prefetchPath, orderRelations);

More info about filtering on prefetchpaths and common mistakes: http://www.llblgening.com/archive/2009/10/prefetchpaths-in-depth/#filteringandsorting

David Elizondo | LLBLGen Support Team