First, use Expression<Func<T, bool>>
instead of Func<T, bool>
.
If you use Func<T, bool>
, you will enumerate before applying your predicate !
It may be enough.
If it's not, add some null checks.
IList<Employee> list = rep.GetItems(
e => e.Department != null && e.Department.Name.Contains("Os")
&& e.Role != null && e.Role.Type == 2,
"Department", "Role"
);
Finally, I would change the implementation of GenericRepository to
class GenericRepository<T> : IRepository<T> where T : class
{
public IList<T> GetItems(Expression<Func<T, bool>> predicate, params string[] navigationProperties)
{
List<T> list;
using(var ctx = new Context())
{
var query = ctx.Set<T>().AsQueryable();
foreach (string navigationProperty in navigationProperties)
query = query.Include(navigationProperty);//got to reaffect it.
list = query.Where(predicate).ToList<T>();
}
return list;
}
}