異常ADO.NET代碼的解釋說明
隨著時(shí)代的進(jìn)步科技的發(fā)展ADO.NET代碼 版本也快速的提升與升級,下面我就對升級前和升級后的ADO.NET代碼,做一下自己的解析。希望能夠?qū)Υ蠹矣袔椭旅婢瓦M(jìn)行解釋說明。
最近在用ADO.NET代碼操作數(shù)據(jù)時(shí)發(fā)現(xiàn)有很多都不能用上,前幾天也發(fā)現(xiàn)一些但沒有記下來,今天又出現(xiàn)了,我決定在博客園里把它記下來。這些功能 在LINQ TO SQL中可以用但在ADO.NET ENTITY FRAMEWORK 中確不能用。
今天在用到模糊查詢時(shí)有用到 in模式的查詢時(shí)突然出現(xiàn)異常代碼如下:
- int[] intArray = {1,2,3,4,5,6,7,8,9,10,11,12,13,14 };
- var query = from a in AdonetContext.Products //Products 為實(shí)體類(表名)
- where intArray.Contains(a.FID)
- select a;
- IEnumable<Product> lists=query.ToList();
當(dāng)執(zhí)行到query.ToList()時(shí)(因?yàn)槭茄舆t執(zhí)行所以會在ToList方法時(shí)才直正執(zhí)行查詢)出現(xiàn)異常:
LINQ to Entities does not recognize the method 'Boolean Contains[Int32](System.Collections.Generic.IEnumerable`1[System.Int32], Int32)' method, and this method cannot be translated into a store expression#t#
而 這句話如果用ADO.NET代碼的話則可以正確執(zhí)行最近生成的SQL語句為:
- exec sp_executesql N'SELECT [t0].[FID], [t0].[FProName], [t0].[FManufacturer], [t0].[FCategory], [t0].[FProDate], [t0].[FPrice]
- FROM [dbo].[Products] AS [t0]
- WHERE [t0].[FID] IN (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11, @p12, @p13)
- ORDER BY [t0].[FID] DESC',N'@p0 int,@p1 int,@p2 int,@p3 int,@p4 int,@p5 int,@p6 int,@p7 int,@p8 int,@p9 int,@p10 int,@p11 int,@p12 int,@p13
- int',@p0=1,@p1=2,@p2=3,@p3=4,@p4=5,@p5=6,@p6=7,@p7=8,@p8=9,@p9=10,@p10=11,@p11=12,@p12=13,@p13=14