简体   繁体   中英

Linq - Using array in Lambda expression to fetch multiple records

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

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM