LINQ prefetch for a query with aggregate

Posts   
 
    
jimph
User
Posts: 63
Joined: 06-Jun-2013
# Posted on: 20-Aug-2013 20:18:12   

Hi, please is there a way to do path prefetch for a query containing aggregate, for example I have these 2 tables

CustomerTable(CustomerId, OrderId, OrderDate) OderDetails(OrderId, Status)

and the query to retrieve customers and their newest order:

        var q = from c in metaData.Customer
                group c by c.CustomerId into g
                let maxDate = g.Max(c => c.OrderDate)
                select g.Where(c => c.OrderDate == maxDate);

I would like to prefetch the OrderDetail using the same query for each selected customer/latest order.

Many thanks

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39912
Joined: 17-Aug-2003
# Posted on: 21-Aug-2013 14:24:00   

Not using group by. You can use order by and take 1 on the prefetch path though, so you fetch the customers, and specify order by on orderdate desc + a limit of 1 on the order prefetch to fetch the latest order with the customer.

Frans Bouma | Lead developer LLBLGen Pro