LINQ 如何实现 in 与 not in

时间:2023-03-08 22:10:13
LINQ 如何实现 in 与 not in
T-SQL的IN:

Select ProductID, ProductName, CategoryID
From dbo.Products
Where CategoryID in (, ) T-SQL的NOT IN: Select ProductID, ProductName, CategoryID
From dbo.Products
Where CategoryID not in (, ) Or Select ProductID, ProductName, CategoryID
From dbo.Products
Where not CategoryID in (, ) LINQ的IN: var queryResult = from p in db.Products
where (new int?[] {,}).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?[] {,}).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)