I'm struggling to find a way to use Include() with some kind of Where condition with passed in lambda expression.
It will be something like this:
Sample usage: userRepository.Get(id, isActive: true, u=>u.UserRoles, u=>u.Group)
public T Get(long id, bool? isActive = true, params Expression<Func<T, object>>[] includes)
{
// Sample usage
// userRepository.Get(id, true, u=>u.UserRoles, u=>u.Group)
IQueryable<T> query = DataContext.Set<T>().Where(_ =>
_.Id == id && (!isActive.HasValue || isActive.Value == _.IsActive));
return
includes.Aggregate(query, (current, includeProperty) => current.Include(includeProperty))
.FirstOrDefault();
}
}
Problem:
All userRoles and group with IsActive = false are included in the result set.
Expectation:
I want to find a way to use Include with Where condition. So it will like this:
userRepository.Get(id, true, u=>u.UserRoles.Where(ur => ur.IsActive == true, u=>u.Group)
I already tried to search in the stack.