Skip to content

Instantly share code, notes, and snippets.

@ismaelgasparin
Created December 27, 2016 01:37
Show Gist options
  • Save ismaelgasparin/7b7ee81e6a8483bf73154668d41f06ee to your computer and use it in GitHub Desktop.
Save ismaelgasparin/7b7ee81e6a8483bf73154668d41f06ee to your computer and use it in GitHub Desktop.
public IQueryable<T> GetAll(params Expression<Func<T, object>>[] includes)
{
IQueryable<T> result = Context.Set<T>();
if (includes.Any())
{
foreach (var include in includes)
{
result = result.Include(include);
}
}
return result;
}
public T Get(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includes)
{
var result = GetAll();
if (includes.Any())
{
foreach (var include in includes)
{
result = result.Include(include);
}
}
return result.FirstOrDefault(predicate);
}
var product = _productRepository.Get(i => i.Id == productId,
i => i.Categories);
var productList = _productRepository.GetAll(i => i.Categories);
@tiagorosendo
Copy link

Acho que voce poderia passar o predicate e os includes direto para o GetAll e depois so dar 1 firstOrDefault, so para nao precisar fazer essa loga do includes.Any() 2 vezes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment