Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq - Using array in Lambda expression to fetch multiple records

Tags:

c#

lambda

linq

Im not sure wether this is possible or not. I'd like to create an array (or list/dictionary) containing some simple id's and the use the array (or whatever) in a lambda expression.

The example below should return the UserId's 15850 and 15858

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

Possible or not?

Thanks

like image 803
Eric Herlitz Avatar asked Sep 18 '11 17:09

Eric Herlitz


1 Answers

It's possible, and will translate into a SQL WHERE IN (...) statement, but it is written kind of backwards in linq:

DbDataContext db = new DbDataContext();    
int[] userIds = {15850, 15858};
var users = db.tblUsers.Where(x => userIds.Contains(x.UserId));
like image 97
Anders Abel Avatar answered Oct 20 '22 01:10

Anders Abel