linq 语句
从网上找到一篇不错的文章,对自己有用,所以记下来了。
希望对大家在以后的项目中能用到,我也是在项目中碰到了这个问题;
本文来自:http://www.dotblogs.com.tw/dc690216/archive/2009/09/13/10601.aspx
算算,接LINQ也有一月的了,可以算是落伍兼新生,不最近在案的候,遇到了在LINQ的Where件式中要如何使用innot in呢!? 候真的只能坐在位子上仰天笑,始念T-SQL其你是最好用滴。之後,了自己日後更方便,於是花了一,考一些路料及MSDN後,得到以下的果:(以下以北料本)
T-SQL的IN: 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))