Select ProductID, ProductName, CategoryID From
dbo.Products Where not 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))
本文深入探讨了SQL和LINQ中IN与NOT IN操作符的应用,通过具体实例展示了如何在数据库查询中使用这些操作符进行筛选与排除。文章详细解析了两种语法在转换为SQL时的等效表达,并提供了相应的SQL查询结果展示。
736

被折叠的 条评论
为什么被折叠?



