Question

I am using EF 4.1, with POCOs which are lazy loaded.

Some sample queries that I run:

var discountsCount = product.Discounts.Count();  
var currentDiscountsCount = product.Discounts.Count(d=>d.IsCurrent);  
var expiredDiscountsCount = product.Discounts.Count(d=>d.IsExpired);  

What I'd like to know, is whether my queries make sense, or are poorly performant:

Am I hitting the database each time, or will the results come from cached data in the DbContext?

Is it okay to access the navigation properties "from scratch" each time, as above, or should I be caching them and then performing more queries on them, for example:

var discounts = product.Discounts;  
var current = discounts.Count(d=>d.IsCurrent);  
var expired = discounts.Count(d=>d.Expired);  

What about a complicated case like below, does it pull the whole collection and then perform local operations on it, or does it construct a specialised SQL query which means that I cannot reuse the results to avoid hitting the database again:

var chained = discounts.OrderBy(d=>d.CreationDate).Where(d=>d.CreationDate < DateTime.Now).Count();

Thanks for the advice!

EDIT based on comments below

So once I call a navigation property (which is a collection), it will load the entire object graph. But what if I filtered that collection using .Count(d=>d...) or Select(d=>d...) or Min(d=>d...), etc. Does it load the entire graph as well, or only the final data?

Was it helpful?

Solution

product.Discounts (or any other navigation collection) isn't an IQueryable but only an IEnumerable. LINQ operations you perform on product.Discounts will never issue a query to the database - with the only exception that in case of lazy loading product.Discounts will be loaded once from the database into memory. It will be loaded completely - no matter which LINQ operation or filter you perform.

If you want to perform filters or any queries on navigation collections without loading the collection completely into memory you must not access the navigation collection but create a query through the context, for instance in your example:

var chained = context.Entry(product).Collection(p => p.Discounts).Query()
                     .Where(d => d.CreationDate < DateTime.Now).Count();

This would not load the Discounts collection of the product into memory but perform a query in the database and then return a single number as result. The next query of this kind would go to the database again.

OTHER TIPS

In your examples above the Discounts collection should be populated by Ef the first time you access it. The subsequent linq queries on the Discount collection should then be performed in memory. This will even include the last complex expression.

You can also use the Include method to make sure you are getting back associated collection first time. example .Include("Discounts");

If your worried about performance I would recommend using SQL Profiler to have a look at what SQL is being executed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top