11

I have a method which should return list of Users if the UserId is in an array. The array of UserIds is passed to the method.

I'm not sure how to write ..where userid in array?

below in ids[] is clearly not correct.

public List<User> GetUsers(int[] ids)
{
   return Users.Values.Where(u => u.UserID in ids[]).ToList();
}

Any ideas how to correct that?

Thanks,

Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
thegunner
  • 6,883
  • 30
  • 94
  • 143

2 Answers2

26

You can try something like that :

public List<User> GetUsers(int[] ids)
{
    return Users.Values.Where(u => ids.Contains(u.UserID)).ToList();
}
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
Quentin Roger
  • 6,410
  • 2
  • 23
  • 36
5

Alternatively to Quentins answer, use this:

public List<User> GetUsers(int[] ids)
{
    return Users.Values.Where(u => ids.Any(x => x == u.UserID)).ToList();
}
B--rian
  • 5,578
  • 10
  • 38
  • 89
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111