-
-
Save oneillci/3205384 to your computer and use it in GitHub Desktop.
public IQueryable<T> FindBy(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includes) | |
{ | |
var query = GetAll().Where(predicate); | |
return includes.Aggregate(query, (current, includeProperty) => current.Include(includeProperty)); | |
} | |
// Sample usage | |
userRepository.FindBy(x => x.Username == username, x.Roles) |
public async Task<IEnumerable> FindBy(Expression<Func<TEntity, bool>> filter, Func<IQueryable, IOrderedQueryable> orderBy = null, params Expression<Func<TEntity, object>>[] includes)
{
try
{
IQueryable query = this.DbSet.Where(filter);
if (orderBy != null)
query = orderBy(query);
includes.Aggregate(query, (current, includeProperty) => current.Include(includeProperty));
return await query.ToListAsync();
}
catch (Exception e)
{
throw e;
}
}
How can I use ThenInclude with this?
I have User class which contains ICollection<Skill>
, where every Skill instance contains ICollection<Entry>
.
FindBy(x => x.Id == 1, x.Skills) works perfect, but how include Entries in every Skill?
They do it here https://github.com/digipolisantwerp/dataaccess_aspnetcore
You can "_context.Set()" instead of GetAll() who have error.
Nice solution thank you ...
public IQueryable Find(Expression<Func<T, bool>> predicate, params Func<IQueryable, IQueryable>[] includes) =>
includes.Aggregate(_dbContext.Set().Where(predicate).AsQueryable(), (current, includesproptity) => includesproptity(current));
_context.Find(a => a.id == id, image =>
{
return image.Include(b => b.PersonImages)
.ThenInclude(a => a.Image);
}, personType =>
{
return personType.Include(a => a.PersonType);
});
Sample usage is wrong should be like this:
// Sample usage
userRepository.FindBy(x => x.Username == username, x=> x.Roles)
How Can I get multiple navigation property
Very nice solution.