Linq where where when id is in an array

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

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

below "in identifiers []" is clearly incorrect.

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

Any ideas how to fix this?

Thank,

+4
source share
2 answers

You can try something like this:

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

Alternatively for Quentins answer use this:

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

All Articles