I'm having trouble running a query on my repository. I have to fetch a product by the id and display it in an edit view alongside the image of the product.
There is a method in my ProductRepository that implements Get() i.e fetch all the product and GetByID as the name implies. I implemented a generic repository pattern with a unit of work class like below
public class GenericRepository<TEntity> where TEntity : class
{
internal SchoolContext context;
internal DbSet<TEntity> dbSet;
public GenericRepository(SchoolContext context)
{
this.context = context;
this.dbSet = context.Set<TEntity>();
}
public virtual IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "")
{
IQueryable<TEntity> query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
if (orderBy != null)
{
return orderBy(query).ToList();
}
else
{
return query.ToList();
}
}
public virtual TEntity GetByID(object id)
{
return dbSet.Find(id);
}
I think thats the only relevant code block. The problem arises when i try to run a query that i found in a tutorial to fetch the product alongside the image with the query below
Product product = db.Products.Include(s => s.Files).SingleOrDefault(s => s.ID == id);
I cant use db.Products because i'm using a unit of work class so i have to run the query with _unit.ProductRepository.GetByID().Include(s => s.Files).SingleOrDefault(s => s.ID == id);
However this does not seem to be possible and i'm stucked.
You can not use Include with a IEnumerable, It only works with IQueryable, when you invoke in your repository
query.ToList();
you query is retrieved from database to memory in a IEnumerable and when your data is in memory Include doesn't work.You can pass the objects that you want to include in your query as a parameter like you do with filter or order in your Get method.
You can override your ProductRepository method
or if you don't want always to return Files
And invoke like