Self Servicing Entities -> Count on Navigator Property

Posts   
 
    
Posts: 10
Joined: 12-Dec-2016
# Posted on: 13-Dec-2016 15:25:49   

I'm working on an older project at work. It is using LLBLGEN. It is referencing SD.LLBLGEN.Pro.DQE.SqlServer.NET20, SD.LLBLGEN.Pro.LinqSupportClasses.NET35, and SD.LLBLGEN.Pro.ORMSupportClasses.NET20,and SD.LLBLGEN.Pro.QuerySpec.

// Code is generated using LLBLGen Pro version: 3.5 // Code is generated using templates: SD.TemplateBindings.SharedTemplates.NET20

My problem is, I noticed our web project was maxing out the servers memory. I ran the program in dotMemory, and found a call that was consuming 910 MB of memory.

There is an MVC view, with a model that has a property Customers which is a collection of CustomerEntity. In the view, it is calling Customer.Orders.Count , and it must be pulling back every single order for every customer that is display in that view (and then doing the count in memory), because that is causing the memory to sky rocket. Watching in the debugger, I see it is doing a select statement and not a count.

What is the proper way to get a count of elements in a navigator property in LLBLGEN?

Thanks!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39613
Joined: 17-Aug-2003
# Posted on: 13-Dec-2016 15:48:55   

A typical lazy loading problem.

The 'Count' property is indeed something that's not translated to a query, it's simply doing the IList.Count so it first pulls all Order entities into memory.

To do a scalar count query, you have a couple of options: (Linking to v5.1, but the usage should also be available in v3.5)

Using QuerySpec (examples how to fetch a scalar): http://www.llblgen.com/Documentation/5.1/LLBLGen%20Pro%20RTF/Using%20the%20generated%20code/QuerySpec/gencode_queryspec_scalarquerycreation.htm

using LInq:

var customerIDsInView = theview.Customers.Select(c=>c.CustomerId).ToList(); // get all customerIds
var orderCount = new LinqMetaData().Order.Where(o=>customerIDsInView.Contains(o.CustomerId)).Count();

Low level API: (examples how to fetch scalars): http://www.llblgen.com/Documentation/5.1/LLBLGen%20Pro%20RTF/Using%20the%20generated%20code/gencode_expressionsaggregates.htm#aggregate-functions-in-scalar-queries

the Linq example I gave is using an in-memory linq query to obtain all customerid's, then creates a query to count all Orders of all customers combined.

This should get you started to work around this lazy loading issue. If you run into problems, just post again in this thread and we help you further.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 10
Joined: 12-Dec-2016
# Posted on: 13-Dec-2016 16:51:08   

Thanks! Unfortunately, I think we've got a few lazy loading problems! What does the GetDbCount function do?

Otis wrote:

A typical lazy loading problem.

The 'Count' property is indeed something that's not translated to a query, it's simply doing the IList.Count so it first pulls all Order entities into memory.

To do a scalar count query, you have a couple of options: (Linking to v5.1, but the usage should also be available in v3.5)

Using QuerySpec (examples how to fetch a scalar): http://www.llblgen.com/Documentation/5.1/LLBLGen%20Pro%20RTF/Using%20the%20generated%20code/QuerySpec/gencode_queryspec_scalarquerycreation.htm

using LInq:

var customerIDsInView = theview.Customers.Select(c=>c.CustomerId).ToList(); // get all customerIds
var orderCount = new LinqMetaData().Order.Where(o=>customerIDsInView.Contains(o.CustomerId)).Count();

Low level API: (examples how to fetch scalars): http://www.llblgen.com/Documentation/5.1/LLBLGen%20Pro%20RTF/Using%20the%20generated%20code/gencode_expressionsaggregates.htm#aggregate-functions-in-scalar-queries

the Linq example I gave is using an in-memory linq query to obtain all customerid's, then creates a query to count all Orders of all customers combined.

This should get you started to work around this lazy loading issue. If you run into problems, just post again in this thread and we help you further.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 14-Dec-2016 03:55:36   

It executes a count on rows.

e.g.

Select Count(*) 
From Orders
Where CustomerID = xyz