Weird Problem

Posts   
 
    
nirataro
User
Posts: 5
Joined: 14-Jun-2006
# Posted on: 24-May-2010 18:59:02   

Hi all,

I am using LLBLGen 2.6 using the adapter model. I have a problem with the following queries (against SQL Server 2008 Express)

var customers = (from customer in meta.Customer.WithPath(p => p.Prefetch<OrderEntity>(y => y.Order)) join order in meta.Order on customer.Id equals order.CustomerId select customer).ToList();

This is a simple join statement for customers with orders.

The problem is that the prefecth returns no results.

So customer.Order always return zero orders. I am quite puzzled by this and at my wits ends. Any suggestion what could be the problem with this simple query?

I checked using a profiler and this is the code generated

SELECT DISTINCT [LPA_L1].[Id], [LPA_L1].[Name], [LPA_L1].[CustomerTypeId], [LPA_L1].[IsEnabled], [LPA_L1].[DateCreated], [LPA_L1].[CreatedBy], [LPA_L1].[LastModified], [LPA_L1].[LastModifiedBy] FROM ( (SELECT [SpeedServices].[dbo].[Customer].[ID] AS [Id], [SpeedServices].[dbo].[Customer].[Name], [SpeedServices].[dbo].[Customer].[CustomerType] AS [CustomerTypeId], [SpeedServices].[dbo].[Customer].[IsEnabled], [SpeedServices].[dbo].[Customer].[DateCreated], [SpeedServices].[dbo].[Customer].[CreatedBy], [SpeedServices].[dbo].[Customer].[LastModified], [SpeedServices].[dbo].[Customer].[LastModifiedBy] FROM [SpeedServices].[dbo].[Customer] ) [LPA_L1] INNER JOIN [SpeedServices].[dbo].[Order] [LPA_L2] ON [LPA_L1].[Id] = [LPA_L2].[CustomerID])

The reverse though works just fine

        var ordersWithCustomer = Query.Get<List<OrderEntity>>(meta =>
            {
                var orders = (from order in meta.Order.WithPath(p => p.Prefetch<CustomerEntity>(y => y.Customer))
                                 join customer in meta.Customer on order.CustomerId equals customer.Id 
                                 select order).ToList();

                return orders;
            });

Prefetch also works fine if I don't join them

       var customersWithOrders = Query.Get<List<CustomerEntity>>(meta =>
            {
                var customers = (from customer in meta.Customer.WithPath(p => p.Prefetch<OrderEntity>(y => y.Order))

                                 select customer).ToList();

                return customers;
            });

With above code, customer.Order returns results.

The following code also works fine

        var customersWithOrders = Query.Get<List<CustomerEntity>>(meta =>
            {
                var customers = (from customer in meta.Customer.WithPath(p => p.Prefetch<OrderEntity>(y => y.Order))

                                 where 
                                 (
                                 from order in meta.Order

                                 select order.CustomerId).Contains(customer.Id)

                                 select customer).ToList();

                return customers;
            });

With above code, customer.Order returns results.

So I wonder if I encounter a limitation in the prefetch capabilities or is there something more?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 25-May-2010 06:33:27   

In your fist query you the the WithPath is specified in an element that not necessary is the final resulset. Your 1:M join could result in a totally different projection. The reverse works because (I think) the relation is M:1. As it could work sometimes, the preferred approach is use WithPath in the final projection. Please read this for more info ("Location of WithPath calls" section).

David Elizondo | LLBLGen Support Team