I have a POCO Domain Entity class that contains convenience methods to navigate to related records. I am using the AdventureWorks2008R2 database to demonstrate what I am attempting to accomplish. All of these queries can be run in LINQPad to observe the SQL statements generated.
SalesOrderHeaders.Where(s => s.SalesOrderID == 43659)
.Single().SalesOrderDetails
This statement produces 2 SQL statements. One for the SalesOrderHeader record and one to retrieve the SalesOrderDetails. Now consider this statement that navigates to an additional related table:
SalesOrderHeaders.Where(s => s.SalesOrderID == 43659)
.Single().SalesOrderDetails.Select(s => s.SpecialOfferProduct)
After retrieving a single SalesOrderHeader record, the domain class would contain a convenience property like this:
public IQueryable<SpecialOfferProduct> SpecialProducts
{
get
{
return SalesOrderDetails.Where(sod => sod.OrderQty > 3)
.Select(s => s.SpecialOfferProduct)
.AsQueryable();
}
}
This statement produces multiple SELECT statements: one for each record in SpecialOfferProduct. My question is: why don't the navigation properties produce a single SELECT statement? This is a huge performance issue because it generates a lot of unnecessary chatter. I could use LINQ SQL syntax, but that is only when making the original query using the Repository. In this case I have an instance of a SalesOrderHeader object and don't have access to the Context or the Repository inside the class. Is there way to force it to create a single SELECT statement using JOINs?
If there is not a way to do it I was thinking of creating an additional method in my Repository to populate these properties. The problem is then I have 2 steps: 1 to retrieve the SalesOrderHeader object, then another to populate the additional properties using the appropriate LINQ statement that would force the JOIN syntax.
Include
method to eagerly load navigation property entities.