码迷,mamicode.com
首页 > 其他好文 > 详细

LINQ 如何实现 in 与 not in

时间:2014-09-15 17:29:09      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   div   sp   cti   log   on   

T-SQL的IN:

Select ProductID, ProductName, CategoryID
From dbo.Products
Where CategoryID in (1, 2)

T-SQL的NOT IN:

Select ProductID, ProductName, CategoryID
From dbo.Products
Where CategoryID not in (1, 2)

Or

Select ProductID, ProductName, CategoryID
From dbo.Products
Where not CategoryID in (1, 2)

LINQ的IN:

var queryResult = from p in db.Products
where (new int?[] {1,2}).Contains(p.CategoryID)
select p;

LINQ的IN解析成SQL:

SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued] 
FROM [dbo].[Products]AS [t0]
WHERE [t0].[CategoryID] IN (@p0, @p1)

LINQ的NOT IN:

var queryResult = from p in db.Products
where ! (new int?[] {1,2}).Contains(p.CategoryID)
select p;

LINQ的NOT IN解析成SQL:

SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued] 
FROM [dbo].[Products]AS [t0]
WHERE NOT [t0].[CategoryID] IN (@p0, @p1)

 

LINQ 如何实现 in 与 not in

标签:style   blog   color   ar   div   sp   cti   log   on   

原文地址:http://www.cnblogs.com/qixuejia/p/3973101.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!