非常简单的查询:
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?