I got the following structure:
One user can have many Groups and a Group can have many users.
Now I want to get all users that are in the same group like the user to filter by.
For example,
The user “Theo” is in Group one and two. I want all users that are in Group one OR Group two.
How can I achive this with LINQ or generally c#?
This code doesn’t work:
var res = (IEnumerable<User>)Users;
foreach (var item in user.Groups) {
res = res.Where(usr => usr.Groups.Contains(item));
}
return res.ToList();
Users is a list I got from another method and user is a parameter of type User.
I assume:
Userclass has a propertyGroupsof typeList<Group>Groupclass has a propertyUsersof typeList<User>Use the LINQ
SelectManymethod:Or in query-syntax:
UPDATE