Linq:在WHERE之后的INNER JOIN而不是之前

时间:2013-10-16 14:21:30

标签: c# sql linq

我有这样的查询:

context.Diffusions.Where(x => x.ProgrammeId == programmeID).Include("Chaines").Include("Version").ToList();

生成的查询是:

SELECT 
[Extent1].[Duree] AS [Duree], 
[Extent1].[Id] AS [Id], 
[Extent1].[ProgrammeId] AS [ProgrammeId], 
[Extent1].[VersionId] AS [VersionId], 
[Extent1].[ChaineId] AS [ChaineId], 
[Extent1].[Debut] AS [Debut], 
[Extent1].[Fin] AS [Fin], 
[Extent1].[ReRun] AS [ReRun], 
[Extent1].[DateModification] AS [DateModification], 
[Extent1].[DateDiffusion] AS [DateDiffusion], 
[Extent2].[Id] AS [Id1], 
[Extent2].[Nom] AS [Nom], 
[Extent2].[Code] AS [Code], 
[Extent2].[Abreviation] AS [Abreviation], 
[Extent3].[Id] AS [Id2], 
[Extent3].[ProgrammeId] AS [ProgrammeId1], 
[Extent3].[CleVersion] AS [CleVersion], 
[Extent3].[Numero] AS [Numero], 
[Extent3].[NumeroModification] AS [NumeroModification], 
[Extent3].[VO] AS [VO], 
[Extent3].[TitrePresse] AS [TitrePresse], 
[Extent3].[Description] AS [Description], 
[Extent3].[Remarque] AS [Remarque], 
[Extent3].[SousTitre] AS [SousTitre], 
[Extent3].[DureeTheorique] AS [DureeTheorique], 
[Extent3].[Format] AS [Format], 
[Extent3].[Interdit] AS [Interdit], 
[Extent3].[LangueId] AS [LangueId], 
[Extent3].[TypeCoteDiffusionId] AS [TypeCoteDiffusionId]
FROM   [dbo].[Diffusion] AS [Extent1]
INNER JOIN [dbo].[Chaine] AS [Extent2] ON [Extent1].[ChaineId] = [Extent2].[Id]
INNER JOIN [dbo].[Version] AS [Extent3] ON [Extent1].[VersionId] = [Extent3].[Id]
WHERE [Extent1].[ProgrammeId] = 1926475

我的问题是Table作为很多条目,它为每个条目创建一个内连接,然后执行“WHERE”,所以它需要6秒。

当我在没有包含它的情况下执行查询时。我希望有一个linq查询执行“WHERE”,然后返回每行的“INCLUDE”,而不必为每个条目手动执行(一个程序可以有1000个扩散)。

1 个答案:

答案 0 :(得分:0)

尝试此代码并使用Contains 例如,按照代码

 context.Diffusions.Where(x => x.ProgrammeId == programmeID).Contains("Chaines").Include("Version").ToList();