As you can see here I have two entities that are linked with each other in two ways.
First way is classic many to many by two ICollection
and second is by entity GroupRole
which specifies role of user for particular group.
The problem is that User
stores his roles for every group and Group
stores roles of each user for itself.
I'd like to retrieve all groups including it's users but only with particular role, unfortunately it seems like when I use Include
statement and then nest inside of it Where
I cannot use Group
entity representation within nested statement and it throws an error
LINQ statement couldn't be translated because of 'g' within Include statement
If anyone could drop some hint how to work around this problem I would be very glad.
Thanks
var result = await groupsRepository.Query(tracking: false)
.Include(g => g.Users.Where(u => u.GroupRoles.Any(r => r.Role == GroupRolesEnum.ExampleRole && r.GroupId == g.GroupId))).
.ToListAsync();
public class Group
{
public string Name { get; set; }
public Guid GroupId { get; set; }
public virtual ICollection<User> Users { get; set; }
public virtual ICollection<GroupRole> UsersRoles { get; set; }
}
public class User
{
public Guid UserId { get; set; }
public string Email { get; set; }
public ICollection<Group> Groups{ get; set; }
public ICollection<GroupRole> GroupRoles { get; set; }
}
public class GroupRole
{
public Guid UserId { get; set; }
public Guid GroupId { get; set; }
public GroupRolesEnum Role { get; set; }
public virtual Group Group{ get; set; }
public virtual User User { get; set; }
}