Moreover, I checked shortly the source code and it seems nop already has build it helpers to call this preload:
namespace Nop.Data
{
/// <summary>
/// Queryable extensions
/// </summary>
public static class QueryableExtensions
{
/// <summary>
/// Include
/// </summary>
/// <typeparam name="T">Type</typeparam>
/// <param name="queryable">Queryable</param>
/// <param name="includeProperties">A list of properties to include</param>
/// <returns>New queryable</returns>
public static IQueryable<T> IncludeProperties<T>(this IQueryable<T> queryable,
params Expression<Func<T, object>>[] includeProperties)
{
if (queryable == null)
throw new ArgumentNullException("queryable");
foreach (Expression<Func<T, object>> includeProperty in includeProperties)
queryable = queryable.Include(includeProperty);
return queryable;
}
}
}
So you can easily optimize your queries with something like:
var query = (from pc in _productCategoryRepository.Table
join p in _productRepository.Table on pc.ProductId equals p.Id
where pc.CategoryId == categoryId &&
!p.Deleted &&
(showHidden || p.Published)
orderby pc.DisplayOrder, pc.Id
select pc)
.IncludeProperties(x => x => x.Product.ProductPictures);
which is equivalent of native LINQ to Entities
var query = (from pc in _productCategoryRepository.Table
join p in _productRepository.Table on pc.ProductId equals p.Id
where pc.CategoryId == categoryId &&
!p.Deleted &&
(showHidden || p.Published)
orderby pc.DisplayOrder, pc.Id
select pc)
.Include(x => x.Product)
.Include(x => x.Product.Select(y => y.ProductPictures))
;
pdunleavy75 wrote:
The category listings and home page would be the main ones to optimise. I'm not sure if eager loading will work considering the repository per entity design pattern used. I think custom LINQ queries or stored procedures that project the data directly into a viewmodel is the way to go. Entity framework is great for line of business CRUD applications (admin), the front end of an e-commerce site is not one of these situations.