Errors with projections in combination with prefetch and/or in-memory execution

Posts   
 
    
Posts: 30
Joined: 19-Dec-2022
# Posted on: 16-Nov-2023 17:16:06   

Hi,

we are using LLBLGen 5.10.1 (Adapter), net4.8/net6.0. Our database is Oracle 11/19 and our target .net version 4.8/netstandard2.0. We are currently migrating from self-servicing to adapter to be able to run our logic against different databases. So far we are using lazy loading at many places and we have update those spots to use prefetch paths where possible. In my research of the different use cases where we have to update from lazy loading to prefetch, I've came across some odd behaviors which seem bugs to me. All include a projection and a combination with prefetch paths and/or in-memory method executions. I've created seperate threads for each of them. FYI: All below behaviors use Adapter. Also I updated all types to be types from the Northwind database to make the examples easier to understand. However I never executed it against Northwind but only to our Entities/database. I hope I didn't make an error though.

1. How to prefetch an entity property which is used inside an in-memory function call? / Prefetch path is ignored.

Consider this code:

bool CustomerHasOrders(CustomerEntity customer)
{
    return customer.Orders.Any();
}

var customerModel = 
  (from c in metaData.Customer.With(c => Orders)
    select new
    {
      Customer = c,
      HasOrders = CustomerHasOrders(c)
     })
   .First();

If I do this neither the customer.Order entities inside of CustomerHasOrders() are prefetched (resulting in HasOrders = false always) nor the order entities in the result model (customerModel.Customer.Orders). It seems that the prefetch is completely ignored. This happens regardless if I use Linq query syntax or linq methods and with With() or WithPath(). It also seems to break down to: prefetch is ignored when having a projection (Select) to a non entity type, because customerModel.Customer.Order is still not prefetched even if I omit the CustomerHasOrders() call.

Is this behavior expected or a bug?

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 17-Nov-2023 00:40:00   

This is an expected behavior as per the docs.

Please read the following section.

Location of WithPath calls

query C doesn't necessarily do so: the prefetch path is inside a query on its own, not on the final set. In query C, the prefetch path definition is ignored

WithPath only works with a query which returns entities. A query which returns a list of anonymous type instances can't be used with WithPath. In that case, use a nested query.

Posts: 30
Joined: 19-Dec-2022
# Posted on: 17-Nov-2023 09:07:32   

I'm sorry, I totally missed that when searching the docs. Thanks for pointing me to it.