简体   繁体   中英

Linq select records that match a list of IDs

Is it possible to change my query below, so that it uses the types list within a contains type query.

So instead of having:

var cust = db.Customers.Where(x => x.type_id==9 || x.type_id==15 || x.type_id==16).ToList();

...I would have something like:

List<int> types = new List<int> { 9, 15, 16 };
var cust = db.Customers.Where(x => types.contains(x.type_id).ToList();

(type_id is not the primary key).

Thank you,

Mark

Yes, method List<T>.Contains will be translated into SQL IN operator:

var cust = db.Customers.Where(x => types.Contains(x.type_id)).ToList();

Generated query will look like:

SELECT * FROM Customers
WHERE type_id IN (@p0, @p1, @p2)

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