I currently have a function that allows me to query the database, including some other related tables in the results, to prevent it from getting back into the database, which, as I know, will happen differently if I do not:
public class EntityRepository<TEntity> : IEntityRepository<TEntity> where TEntity : class
{
protected DbSet<TEntity> dbSet;
private readonly DbContext dc;
public EntityRepository()
{
dc = new DbContext(Utility.Config.GetEntityDbConnection());
dbSet = dc.Set<TEntity>();
}
public IQueryable<TEntity> GetAllIncluding(params Expression<Func<TEntity, object>>[] includeProperties)
{
foreach (var includedProperty in includeProperties)
{
dbSet.Include(includedProperty).Load();
}
return dbSet;
}
}
However, I need to also use the where clause before turning it on so that it does not try to query the entire database when the method starts Load().
I tried to do something like this (which obviously does not work, because you can reassign dbset, as in my code example below).
public IQueryable<TEntity> SearchForIncluding(Expression<Func<TEntity, bool>> predicate, params Expression<Func<TEntity, object>>[] includeProperties)
{
dbset = dbSet.Where(predicate);
foreach (var includedProperty in includeProperties)
{
dbSet.Include(includedProperty).Load();
}
}
Richc source
share