EF:在LINQ查询中使用Contains时,什么是SQL输出? [英] EF: What is the SQL output when using Contains in LINQ query?

查看:475
本文介绍了EF:在LINQ查询中使用Contains时,什么是SQL输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在NHibernate中,当我们执行一个如下生成的SQL查询时,它将包含集合中每个元素的参数。如果查询是针对MSSQL Server执行的,并且集合中有2k或更多的元素,则会收到错误,因为sql引擎允许的最大参数为2k。

In NHibernate when we execute a query like the one below the SQL generated for it will contains parameters for each element in the collection. If the query is executed against MSSQL Server and there are 2k or more elements in the collection you will get an error because max parameters allowed by the sql engine is 2k.

var bankaccounts = from b in this.unitOfWork.BankAccounts
                    where command.Ids.Contains(b.Id) // command.ids is an array with id's
                    select b;

对于因为EntityFramework而懒惰的测试,我看到其他开发人员编写这样的代码: EF因为关联而无法删除子对象

Sorry for being lazy to test this myself with EntityFramework but I see other developers writing such code: EF cannot delete child object because of associationset

问题是这些查询会从EF生成什么SQL?

The question is what SQL will be generated from EF for this kind of queries?

推荐答案

p>我已经看过你的SQL分析器了。这是在Entity框架中发生的事情(我想这是NHibernate也会产品,但我不知道..):

I've watched SQL profiler for you. This is what happens in Entity Framework (I quess this is wat NHibernate also will product, but I'm not sure..):

SELECT 
[Extent1].[Id] AS [Id], 
[Extent1].[AccountNo] AS [AccountNo], 
[Extent1].[Name] AS [Name], 
[Extent1].[Description] AS [Description], 
[Extent1].[IBAN] AS [IBAN], 
[Extent1].[IsActive] AS [IsActive], 
[Extent1].[Customer_Id] AS [Customer_Id]
FROM [dbo].[BankAccounts] AS [Extent1]
WHERE [Extent1].[Id] IN (4,5,6,7,9)

如您所见,它将成为一个 WHERE。 。IN([包含值])

As you can see, it will become a WHERE .. IN ([the contains values])

这篇关于EF:在LINQ查询中使用Contains时,什么是SQL输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆