为什么EF中的LinqToSql总是在子查询中生成EXISTS而不是IN?

时间:2015-04-14 07:04:05

标签: c# entity-framework linq-to-sql

非常简单的查询:

var test = _repository.Companies.AllItems.Where(el => companyIdxQuery.Contains(el.Id)).ToList();
var companyIdxQuery = _repository.CompanyFolderIndexes.Items.Select(el => el.CompanyId);

生成SQL,在我的数据库上执行40秒,其中具有IN构造的等效SQL立即执行。这是它真正产生的:

SELECT 
  [Extent1].[Id] AS [Id], 
  [Extent1].[Name] AS [Name], 
  FROM [dbo].[Companies] AS [Extent1]
WHERE  EXISTS (SELECT 
    1 AS [C1]
    FROM [dbo].[CompanyFolderIndexes] AS [Extent2]
    WHERE [Extent2].[CompanyId] = [Extent1].[Id]
)

何以使EF使用IN生成SQL?

0 个答案:

没有答案