Created
July 30, 2012 06:45
-
-
Save oneillci/3205384 to your computer and use it in GitHub Desktop.
Using EF Include() with generic repository
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can "_context.Set()" instead of GetAll() who have error.
Nice solution thank you ...