Reputation: 67
I have this query
var val = from user in allUsers where currentUserTeams.Contains(user.Teams) select user;
where allusers
contains each user with Teams
object, which is a list of team id and currentUserTeams
contains list of team id. i used above linq to find out if any of user is part of team of currentuserteams team id but does not seem to be working
Upvotes: 1
Views: 1087
Reputation: 8301
Your question is not really clear, if you mean by :
contains each
user
withTeams
which is a list ofteam id
andcurrentUserTeams
contains list ofteam id
.
and
I used above linq to find out if any of
user
is part of team ofcurrentuserteams
team id
1. That both lists contain squarely Ids
and that you want to check if any user of the allUsers
is part of one of the currentUserTeams
, then you should go for :
var val = (from user in allUsers
where currentUserTeams.Any(curentUserTeamId=>user.Teams.Any(teamId=>teamId == curentUserTeamId)
select user).ToList();
Or
2. That both lists contain Team
s and that you want to check if any of the allUsers
is part of one of the currentUserTeams
, then you should go for :
var val = (from user in allUsers
where currentUserTeams.Any(curentUserTeam=>user.Teams.Any(team=>team.Id == curentUserTeam.Id)
Select).ToList();
Update : Using Foreach :
var memberList = new List<User>();
AllUsers.Foreach(user=>
{
user.Teams.Foreach(id=>
{
if(currentUserTeams.Contains(id)
{
memberList.Add(user);
Break; //Because you don't need to loop through the rest of the sequence
}
})
})
Upvotes: 0
Reputation: 133403
Assuming you have ID
as Team ID property, You can use Enumerable.Any<TSource> Method (IEnumerable<TSource>, Func<TSource, Boolean>)
to check whether any element of a sequence satisfies a condition.
var val = allUsers
.Where(u =>
u.Teams.Any(t => currentUserTeams.Any(c => c == t.Id)
)
.ToList();
Upvotes: 2