I follow repository pattern and I have the following method in my generic repository class:
public virtual T Get(Expression<Func<T, bool>> where)
{
return dbset.Where(where).FirstOrDefault<T>();
}
I would like to add the a lambda expression for including navigation properties. Is it possible?
There's my generic repo Get method:
public virtual IQueryable<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);
}
if (!string.IsNullOrEmpty(includeProperties))
{
foreach (string IncludeProperty in includeProperties.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries))
{
Query = Query.Include(IncludeProperty);
}
}
if (orderBy != null)
{
return orderBy(Query);
}
return Query;
}