Counting number of child records in a projection

Posts   
 
    
AlbertK
User
Posts: 44
Joined: 23-Dec-2009
# Posted on: 29-Oct-2010 16:26:45   

I'm trying to figure the best way to count number of child records inside a projection. LinqSupportClasses : 2.6.9.220 ORSMSupportClasses: 2.6.9.305

Could you explain why the following does not work. I'm using adapter model.


var q = from c in metaData.Customer.WithPath(p=>p.Prefetch(c=>c.Orders))
        select new
        {
            Name = c.ContactName,
            NumOrders = c.Orders.Count //Count causes a problem
        };

I found the following workaround, but I like the above syntax much better disappointed


var q = from c in metaData.Customer
        select new
        {
            Name = c.ContactName,
            NumOrders = 
                    (from o in metaData.Orders
                    where o.CustomerId == c.CustomerId
                    select o).Count()
        };

Thank you.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39906
Joined: 17-Aug-2003
# Posted on: 29-Oct-2010 17:15:07   

have you tried: var q = from c in metaData.Customer.WithPath(p=>p.Prefetch(c=>c.Orders)) select new { Name = c.ContactName, NumOrders = c.Orders.Count() };

so the extension method instead of the property? what exact problem is reported? (your runtime build is slightly outdated as well, we made some linq fixes since then. It might not be relevant for this problem, but it can prevent you from running into an issue which has been fixed in a newer build wink )

Frans Bouma | Lead developer LLBLGen Pro
AlbertK
User
Posts: 44
Joined: 23-Dec-2009
# Posted on: 29-Oct-2010 17:23:28   

Count() it is. I don't know how I missed it. The error message even said - could it be Count vs Count()?

flushed

I will also get the latest runtime. Thank you so much!