We are using Entity framework along with the generic repository pattern.
When loading multiple entities we use includeProperties string filter in generic repository pattern.
Following is the code I have in my service class,
customerRepository.Get (filter: c => ! c.IsDeleted, includeProperties : "Orders");
Following is the code I have in my Generic Repository
public virtual IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "",
Func<TEntity, TEntity> selectColumns = null)
{
IQueryable<TEntity> query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
if (selectColumns != null)
{
query = query.Select(selectColumns).AsQueryable();
}
query = includeProperties.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Aggregate(query, (current, includeProperty) => current.Include(includeProperty));
return orderBy != null ? orderBy(query).ToList() : query.ToList();
}
It is possible that a customer can have multiple orders which are already deleted. However using above includeProperties we are only able to load all orders of a customer.
How can we load only non deleted orders of a customer?
i.e. How can we apply conditional include using includeProperties in generaic repository pattern?
We tried the code below but it did not work:
customerRepository.Get (filter: c => ! c.IsDeleted && ! c.Orders.IsDeleted, includeProperties : "Orders");