Convert low-level API with Prefetch to Linq

Posts   
 
    
iDave avatar
iDave
User
Posts: 67
Joined: 14-Jun-2010
# Posted on: 19-Mar-2014 09:35:11   

Good day,

I consider myself new to Linq and only have done a few basic queries with no problem with the exception of this last one.

My question is pretty simple, how I should write the following low-level API as Linq with Lambda expression:


var filter = new RelationPredicateBucket(OrderFields.Number == number);

var path = new PrefetchPath2(SBC.DAL.EntityType.OrderEntity);

path.Add(OrderEntity.PrefetchPathOrderDetails).SubPath.Add(OrderDetailEntity.PrefetchPathInventory).SubPath.Add(InventoryEntity.PrefetchPathProduct)
.SubPath.Add(ProductEntity.PrefetchPathProductUnit);
path.Add(OrderEntity.PrefetchPathCustomer);
path.Add(OrderEntity.PrefetchPathEmployee);
path.Add(OrderEntity.PrefetchPathCompany);

I don't have a problem with the above code and it works flawlessly, I'm just asking since I would like to know how to do it the other way. simple_smile

This is what I have done so far:


var metaData = new LinqMetaData(adapter);
var q = (from o in metaData.Order
         where o.Number == number
         select o).WithPath(orderPath => orderPath
                    .Prefetch<OrderDetailEntity>(o => o.OrderDetails)
                        .SubPath(orderDetailPath => orderDetailPath
                            .Prefetch<InventoryEntity>(od => od.Inventory)
                                .SubPath(inventoryPath => inventoryPath
                                    .Prefetch<ProductEntity>(i => i.Product)
                                        .SubPath(productPath => productPath
                                            .Prefetch<ProductUnitEntity>(p => p.ProductUnit)))));

Which translates to this:


var filter = new RelationPredicateBucket(OrderFields.Number == number);

var path = new PrefetchPath2(SBC.DAL.EntityType.OrderEntity);

path.Add(OrderEntity.PrefetchPathOrderDetails).SubPath.Add(OrderDetailEntity.PrefetchPathInventory).SubPath.Add(InventoryEntity.PrefetchPathProduct)
.SubPath.Add(ProductEntity.PrefetchPathProductUnit);

Thanks in advance, Dave

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 20-Mar-2014 06:40:34   
var metaData = new LinqMetaData(adapter);
var q = (from o in metaData.Order
         where o.Number == number
         select o)
         .WithPath(orderPath => orderPath
                    .Prefetch<OrderDetailEntity>(o => o.OrderDetails)
                        .SubPath(orderDetailPath => orderDetailPath
                            .Prefetch<InventoryEntity>(od => od.Inventory)
                                .SubPath(inventoryPath => inventoryPath
                                    .Prefetch<ProductEntity>(i => i.Product)
                                        .SubPath(productPath => productPath
                                            .Prefetch<ProductUnitEntity>(p => p.ProductUnit))))
                    .Prefetch<CustomerEntity>(o=>o.Customer))
                    .Prefetch<EmployeeEntity>(o=>o.Employee))
                    .Prefetch<CompanyEntity>(o=>o.Company))
);
iDave avatar
iDave
User
Posts: 67
Joined: 14-Jun-2010
# Posted on: 06-Apr-2014 07:11:01   

Thanks a lot Walaa! That did the trick... I was doing it wrong, instead of adding Prefetch I was adding again WithPath.

Anyway, just a small correction, there are extra ')' in the code, it should be:


.Prefetch<CustomerEntity>(o => o.Customer)
.Prefetch<EmployeeEntity>(o => o.Employee)
.Prefetch<CompanyEntity>(o => o.Company)

wink

Thanks again!