问题描述
T-SQL中写in与not in语句很方便:
select * from Products where CategoryID in(1,2)
select * from Products where CategoryID not in(1,2),
但是在Linq中如何实现呢?
解决方案
// LINQ的IN:
var queryResult = from p in db.Products
where (new int?[] {1,2}).Contains(p.CategoryID)
select p;
// LINQ的NOT IN:
var queryResult = from p in db.Products
where ! (new int?[] {1,2}).Contains(p.CategoryID)
select p;