Linq - using an array in a Lambda expression to retrieve multiple records

Not sure if this is possible or not. I would like to create an array (or list / dictionary) containing some simple id, and use an array (or something else) in a lambda expression.

In the example below, UserId 15850 and 15858 should be returned

DbDataContext db = new DbDataContext();    
int[] userIds = {15850, 15858};
var users = db.tblUsers.Where(x => x.UserId.Equals(userIds));

Is it possible or not?

thank

+5
source share
1 answer

It is possible, and it will be translated into an SQL statement WHERE IN (...), but it is written in reverse order in linq:

DbDataContext db = new DbDataContext();    
int[] userIds = {15850, 15858};
var users = db.tblUsers.Where(x => userIds.Contains(x.UserId));
+8
source

All Articles