__The following query :__
```
{SELECT
[Extent1].[Id] AS [Id],
[Extent1].[No] AS [No],
[Extent1].[ShortTitle] AS [ShortTitle],
[Extent10].[Title] AS [Title],
[Extent11].[Title] AS [Title1],
[Extent1].[IranCodeNo] AS [IranCodeNo],
[Extent1].[IsService] AS [IsService],
[Extent1].[IsApproved] AS [IsApproved],
[Extent12].[Title] AS [Title2],
[Extent13].[Title] AS [Title3],
[Join7].[Title1] AS [Title4],
[Join7].[No1] AS [No1]
FROM [dbo].[Products] AS [Extent1]
LEFT OUTER JOIN (SELECT [Extent2].[Id] AS [Id1], [Extent5].[No] AS [No1], [Extent5].[Title] AS [Title1], [Extent9].[OrganizationPeriodId] AS [OrganizationPeriodId]
FROM [dbo].[Products] AS [Extent2]
LEFT OUTER JOIN [dbo].[ResourceProducts] AS [Extent3] ON [Extent2].[Id] = [Extent3].[ProductId]
LEFT OUTER JOIN [dbo].[ResourceRoots] AS [Extent4] ON [Extent3].[ResourceRootId] = [Extent4].[Id]
LEFT OUTER JOIN [dbo].[Resources] AS [Extent5] ON [Extent4].[Id] = [Extent5].[ResourceRootId]
LEFT OUTER JOIN [dbo].[ResourceSubGroups] AS [Extent6] ON [Extent5].[ResourceSubGroupId] = [Extent6].[Id]
LEFT OUTER JOIN [dbo].[ResourceGroups] AS [Extent7] ON [Extent6].[ResourceGroupId] = [Extent7].[Id]
LEFT OUTER JOIN [dbo].[ResourceCategories] AS [Extent8] ON [Extent7].[ResourceCategoryId] = [Extent8].[Id]
INNER JOIN [dbo].[ResourceTypes] AS [Extent9] ON [Extent8].[ResourceTypeId] = [Extent9].[Id] ) AS [Join7] ON ([Join7].[OrganizationPeriodId] = (CASE WHEN (@p__linq__0 = 1) THEN @p__linq__1 ELSE 0 END)) AND ([Extent1].[Id] = [Join7].[Id1])
LEFT OUTER JOIN (SELECT
[vwUnits].[Id] AS [Id],
[vwUnits].[AttributeGroupId] AS [AttributeGroupId],
[vwUnits].[Title] AS [Title],
[vwUnits].[LanguageId] AS [LanguageId],
[vwUnits].[ParentId] AS [ParentId],
[vwUnits].[Order] AS [Order]
FROM [dbo].[vwUnits] AS [vwUnits]) AS [Extent10] ON [Extent1].[UnitCode] = [Extent10].[Id]
LEFT OUTER JOIN (SELECT
[vwCountries].[Id] AS [Id],
[vwCountries].[AttributeGroupId] AS [AttributeGroupId],
[vwCountries].[Title] AS [Title],
[vwCountries].[ParentId] AS [ParentId],
[vwCountries].[Order] AS [Order]
FROM [dbo].[vwCountries] AS [vwCountries]) AS [Extent11] ON [Extent1].[CountryCode] = [Extent11].[Id]
LEFT OUTER JOIN [dbo].[Manufacturers] AS [Extent12] ON [Extent1].[ManufacturerId] = [Extent12].[Id]
LEFT OUTER JOIN (SELECT
[vwProductGroups].[Id] AS [Id],
[vwProductGroups].[AttributeGroupId] AS [AttributeGroupId],
[vwProductGroups].[Title] AS [Title],
[vwProductGroups].[LanguageId] AS [LanguageId],
[vwProductGroups].[ParentId] AS [ParentId],
[vwProductGroups].[Order] AS [Order]
FROM [dbo].[vwProductGroups] AS [vwProductGroups]) AS [Extent13] ON [Extent1].[ProductGroupCode] = [Extent13].[Id]}
```
__generates this sql which takes time 15 seconds to run! :__
```
exec sp_executesql N'SELECT TOP (10)
[Project1].[Id] AS [Id],
[Project1].[No] AS [No],
[Project1].[ShortTitle] AS [ShortTitle],
[Project1].[Title] AS [Title],
[Project1].[Title1] AS [Title1],
[Project1].[IranCodeNo] AS [IranCodeNo],
[Project1].[IsService] AS [IsService],
[Project1].[IsApproved] AS [IsApproved],
[Project1].[Title2] AS [Title2],
[Project1].[Title3] AS [Title3],
[Project1].[Title4] AS [Title4],
[Project1].[No1] AS [No1]
FROM ( SELECT [Project1].[Id] AS [Id], [Project1].[No] AS [No], [Project1].[ShortTitle] AS [ShortTitle], [Project1].[IranCodeNo] AS [IranCodeNo], [Project1].[IsService] AS [IsService], [Project1].[IsApproved] AS [IsApproved], [Project1].[Title] AS [Title], [Project1].[Title1] AS [Title1], [Project1].[Title2] AS [Title2], [Project1].[Title3] AS [Title3], [Project1].[No1] AS [No1], [Project1].[Title4] AS [Title4], row_number() OVER (ORDER BY [Project1].[Id] ASC) AS [row_number]
FROM ( SELECT
[Extent1].[Id] AS [Id],
[Extent1].[No] AS [No],
[Extent1].[ShortTitle] AS [ShortTitle],
[Extent1].[IranCodeNo] AS [IranCodeNo],
[Extent1].[IsService] AS [IsService],
[Extent1].[IsApproved] AS [IsApproved],
[Extent10].[Title] AS [Title],
[Extent11].[Title] AS [Title1],
[Extent12].[Title] AS [Title2],
[Extent13].[Title] AS [Title3],
[Join7].[No2] AS [No1],
[Join7].[Title5] AS [Title4]
FROM [dbo].[Products] AS [Extent1]
LEFT OUTER JOIN (SELECT [Extent2].[Id] AS [Id1], [Extent5].[No] AS [No2], [Extent5].[Title] AS [Title5], [Extent9].[OrganizationPeriodId] AS [OrganizationPeriodId]
FROM [dbo].[Products] AS [Extent2]
LEFT OUTER JOIN [dbo].[ResourceProducts] AS [Extent3] ON [Extent2].[Id] = [Extent3].[ProductId]
LEFT OUTER JOIN [dbo].[ResourceRoots] AS [Extent4] ON [Extent3].[ResourceRootId] = [Extent4].[Id]
LEFT OUTER JOIN [dbo].[Resources] AS [Extent5] ON [Extent4].[Id] = [Extent5].[ResourceRootId]
LEFT OUTER JOIN [dbo].[ResourceSubGroups] AS [Extent6] ON [Extent5].[ResourceSubGroupId] = [Extent6].[Id]
LEFT OUTER JOIN [dbo].[ResourceGroups] AS [Extent7] ON [Extent6].[ResourceGroupId] = [Extent7].[Id]
LEFT OUTER JOIN [dbo].[ResourceCategories] AS [Extent8] ON [Extent7].[ResourceCategoryId] = [Extent8].[Id]
INNER JOIN [dbo].[ResourceTypes] AS [Extent9] ON [Extent8].[ResourceTypeId] = [Extent9].[Id] ) AS [Join7] ON ([Join7].[OrganizationPeriodId] = (CASE WHEN (@p__linq__0 = 1) THEN @p__linq__1 ELSE 0 END)) AND ([Extent1].[Id] = [Join7].[Id1])
LEFT OUTER JOIN (SELECT
[vwUnits].[Id] AS [Id],
[vwUnits].[AttributeGroupId] AS [AttributeGroupId],
[vwUnits].[Title] AS [Title],
[vwUnits].[LanguageId] AS [LanguageId],
[vwUnits].[ParentId] AS [ParentId],
[vwUnits].[Order] AS [Order]
FROM [dbo].[vwUnits] AS [vwUnits]) AS [Extent10] ON [Extent1].[UnitCode] = [Extent10].[Id]
LEFT OUTER JOIN (SELECT
[vwCountries].[Id] AS [Id],
[vwCountries].[AttributeGroupId] AS [AttributeGroupId],
[vwCountries].[Title] AS [Title],
[vwCountries].[ParentId] AS [ParentId],
[vwCountries].[Order] AS [Order]
FROM [dbo].[vwCountries] AS [vwCountries]) AS [Extent11] ON [Extent1].[CountryCode] = [Extent11].[Id]
LEFT OUTER JOIN [dbo].[Manufacturers] AS [Extent12] ON [Extent1].[ManufacturerId] = [Extent12].[Id]
LEFT OUTER JOIN (SELECT
[vwProductGroups].[Id] AS [Id],
[vwProductGroups].[AttributeGroupId] AS [AttributeGroupId],
[vwProductGroups].[Title] AS [Title],
[vwProductGroups].[LanguageId] AS [LanguageId],
[vwProductGroups].[ParentId] AS [ParentId],
[vwProductGroups].[Order] AS [Order]
FROM [dbo].[vwProductGroups] AS [vwProductGroups]) AS [Extent13] ON [Extent1].[ProductGroupCode] = [Extent13].[Id]
) AS [Project1]
) AS [Project1]
WHERE [Project1].[row_number] > 11490
ORDER BY [Project1].[Id] ASC',N'@p__linq__0 int,@p__linq__1 int',@p__linq__1=2380,@p__linq__0= 1
```
__but the exactly same direct query runs in less than a second! I replaced "CASE WHEN (@p__linq__0 = 1)" with "CASE WHEN 1 = 1" in the above query and everything works great and fast after that!__
Comments: Sure! : ``` var q = from r in (ProductRepository.AllIncluding(product => product.Manufacturer, product => product.Country, product => product.Unit, product => product.ProductGroup)) select r; .... var q2 = from r in ProductRepository.All join rp in ResourceProductRepository.All on r.Id equals rp.ProductId into g1 from r2 in g1.DefaultIfEmpty() join rr in ResourceRepository.AllRoots on r2.ResourceRootId equals rr.Id into g2 from r3 in g2.DefaultIfEmpty() join r4 in ResourceRepository.All on r3.Id equals r4.ResourceRootId into g3 from resource in g3.DefaultIfEmpty() where resource.ResourceSubGroup.ResourceGroup.ResourceCategory.ResourceType.OrganizationPeriodId == (Commons.CurrentUser.IsActivePeriodLoaded ? Commons.CurrentUser.ActivePeriodId : 0) select new { ProductId = r.Id, ResourceNo = resource.No, ResourceTitle = resource.Title }; return from r in q join r2 in q2 on r.Id equals r2.ProductId into g from resource in g.DefaultIfEmpty() select new ProductGridRowViewModel { Id = r.Id, No = r.No, ShortTitle = r.ShortTitle, UnitTitle = r.Unit.Title, CountryTitle = r.Country.Title, IranCodeNo = r.IranCodeNo, IsService = r.IsService, IsApproved = r.IsApproved, ManufacturerTitle = r.Manufacturer.Title, ProductGroupTitle = r.ProductGroup.Title, RelatedResourceTitle = resource.ResourceTitle, RelatedResourceNo = resource.ResourceNo, }; ``` the problem is with this part : ``` resource.ResourceSubGroup.ResourceGroup.ResourceCategory.ResourceType.OrganizationPeriodId == (Commons.CurrentUser.IsActivePeriodLoaded ? Commons.CurrentUser.ActivePeriodId : 0) ``` The work-around was to check this condition in code and put it into a new variable , so I will have something like this : ``` int activePeriodId = Commons.CurrentUser.IsActivePeriodLoaded ? Commons.CurrentUser.ActivePeriodId : 0;//NOTE! this appraoch(using a variable instead of using it directly in the query) helps EntityFramework to run 30 times faster! (less than a second instead of 15 seconds!!!) //and in where clause : .... where resource.ResourceSubGroup.ResourceGroup.ResourceCategory.ResourceType.OrganizationPeriodId == activePeriodId ... ```
```
{SELECT
[Extent1].[Id] AS [Id],
[Extent1].[No] AS [No],
[Extent1].[ShortTitle] AS [ShortTitle],
[Extent10].[Title] AS [Title],
[Extent11].[Title] AS [Title1],
[Extent1].[IranCodeNo] AS [IranCodeNo],
[Extent1].[IsService] AS [IsService],
[Extent1].[IsApproved] AS [IsApproved],
[Extent12].[Title] AS [Title2],
[Extent13].[Title] AS [Title3],
[Join7].[Title1] AS [Title4],
[Join7].[No1] AS [No1]
FROM [dbo].[Products] AS [Extent1]
LEFT OUTER JOIN (SELECT [Extent2].[Id] AS [Id1], [Extent5].[No] AS [No1], [Extent5].[Title] AS [Title1], [Extent9].[OrganizationPeriodId] AS [OrganizationPeriodId]
FROM [dbo].[Products] AS [Extent2]
LEFT OUTER JOIN [dbo].[ResourceProducts] AS [Extent3] ON [Extent2].[Id] = [Extent3].[ProductId]
LEFT OUTER JOIN [dbo].[ResourceRoots] AS [Extent4] ON [Extent3].[ResourceRootId] = [Extent4].[Id]
LEFT OUTER JOIN [dbo].[Resources] AS [Extent5] ON [Extent4].[Id] = [Extent5].[ResourceRootId]
LEFT OUTER JOIN [dbo].[ResourceSubGroups] AS [Extent6] ON [Extent5].[ResourceSubGroupId] = [Extent6].[Id]
LEFT OUTER JOIN [dbo].[ResourceGroups] AS [Extent7] ON [Extent6].[ResourceGroupId] = [Extent7].[Id]
LEFT OUTER JOIN [dbo].[ResourceCategories] AS [Extent8] ON [Extent7].[ResourceCategoryId] = [Extent8].[Id]
INNER JOIN [dbo].[ResourceTypes] AS [Extent9] ON [Extent8].[ResourceTypeId] = [Extent9].[Id] ) AS [Join7] ON ([Join7].[OrganizationPeriodId] = (CASE WHEN (@p__linq__0 = 1) THEN @p__linq__1 ELSE 0 END)) AND ([Extent1].[Id] = [Join7].[Id1])
LEFT OUTER JOIN (SELECT
[vwUnits].[Id] AS [Id],
[vwUnits].[AttributeGroupId] AS [AttributeGroupId],
[vwUnits].[Title] AS [Title],
[vwUnits].[LanguageId] AS [LanguageId],
[vwUnits].[ParentId] AS [ParentId],
[vwUnits].[Order] AS [Order]
FROM [dbo].[vwUnits] AS [vwUnits]) AS [Extent10] ON [Extent1].[UnitCode] = [Extent10].[Id]
LEFT OUTER JOIN (SELECT
[vwCountries].[Id] AS [Id],
[vwCountries].[AttributeGroupId] AS [AttributeGroupId],
[vwCountries].[Title] AS [Title],
[vwCountries].[ParentId] AS [ParentId],
[vwCountries].[Order] AS [Order]
FROM [dbo].[vwCountries] AS [vwCountries]) AS [Extent11] ON [Extent1].[CountryCode] = [Extent11].[Id]
LEFT OUTER JOIN [dbo].[Manufacturers] AS [Extent12] ON [Extent1].[ManufacturerId] = [Extent12].[Id]
LEFT OUTER JOIN (SELECT
[vwProductGroups].[Id] AS [Id],
[vwProductGroups].[AttributeGroupId] AS [AttributeGroupId],
[vwProductGroups].[Title] AS [Title],
[vwProductGroups].[LanguageId] AS [LanguageId],
[vwProductGroups].[ParentId] AS [ParentId],
[vwProductGroups].[Order] AS [Order]
FROM [dbo].[vwProductGroups] AS [vwProductGroups]) AS [Extent13] ON [Extent1].[ProductGroupCode] = [Extent13].[Id]}
```
__generates this sql which takes time 15 seconds to run! :__
```
exec sp_executesql N'SELECT TOP (10)
[Project1].[Id] AS [Id],
[Project1].[No] AS [No],
[Project1].[ShortTitle] AS [ShortTitle],
[Project1].[Title] AS [Title],
[Project1].[Title1] AS [Title1],
[Project1].[IranCodeNo] AS [IranCodeNo],
[Project1].[IsService] AS [IsService],
[Project1].[IsApproved] AS [IsApproved],
[Project1].[Title2] AS [Title2],
[Project1].[Title3] AS [Title3],
[Project1].[Title4] AS [Title4],
[Project1].[No1] AS [No1]
FROM ( SELECT [Project1].[Id] AS [Id], [Project1].[No] AS [No], [Project1].[ShortTitle] AS [ShortTitle], [Project1].[IranCodeNo] AS [IranCodeNo], [Project1].[IsService] AS [IsService], [Project1].[IsApproved] AS [IsApproved], [Project1].[Title] AS [Title], [Project1].[Title1] AS [Title1], [Project1].[Title2] AS [Title2], [Project1].[Title3] AS [Title3], [Project1].[No1] AS [No1], [Project1].[Title4] AS [Title4], row_number() OVER (ORDER BY [Project1].[Id] ASC) AS [row_number]
FROM ( SELECT
[Extent1].[Id] AS [Id],
[Extent1].[No] AS [No],
[Extent1].[ShortTitle] AS [ShortTitle],
[Extent1].[IranCodeNo] AS [IranCodeNo],
[Extent1].[IsService] AS [IsService],
[Extent1].[IsApproved] AS [IsApproved],
[Extent10].[Title] AS [Title],
[Extent11].[Title] AS [Title1],
[Extent12].[Title] AS [Title2],
[Extent13].[Title] AS [Title3],
[Join7].[No2] AS [No1],
[Join7].[Title5] AS [Title4]
FROM [dbo].[Products] AS [Extent1]
LEFT OUTER JOIN (SELECT [Extent2].[Id] AS [Id1], [Extent5].[No] AS [No2], [Extent5].[Title] AS [Title5], [Extent9].[OrganizationPeriodId] AS [OrganizationPeriodId]
FROM [dbo].[Products] AS [Extent2]
LEFT OUTER JOIN [dbo].[ResourceProducts] AS [Extent3] ON [Extent2].[Id] = [Extent3].[ProductId]
LEFT OUTER JOIN [dbo].[ResourceRoots] AS [Extent4] ON [Extent3].[ResourceRootId] = [Extent4].[Id]
LEFT OUTER JOIN [dbo].[Resources] AS [Extent5] ON [Extent4].[Id] = [Extent5].[ResourceRootId]
LEFT OUTER JOIN [dbo].[ResourceSubGroups] AS [Extent6] ON [Extent5].[ResourceSubGroupId] = [Extent6].[Id]
LEFT OUTER JOIN [dbo].[ResourceGroups] AS [Extent7] ON [Extent6].[ResourceGroupId] = [Extent7].[Id]
LEFT OUTER JOIN [dbo].[ResourceCategories] AS [Extent8] ON [Extent7].[ResourceCategoryId] = [Extent8].[Id]
INNER JOIN [dbo].[ResourceTypes] AS [Extent9] ON [Extent8].[ResourceTypeId] = [Extent9].[Id] ) AS [Join7] ON ([Join7].[OrganizationPeriodId] = (CASE WHEN (@p__linq__0 = 1) THEN @p__linq__1 ELSE 0 END)) AND ([Extent1].[Id] = [Join7].[Id1])
LEFT OUTER JOIN (SELECT
[vwUnits].[Id] AS [Id],
[vwUnits].[AttributeGroupId] AS [AttributeGroupId],
[vwUnits].[Title] AS [Title],
[vwUnits].[LanguageId] AS [LanguageId],
[vwUnits].[ParentId] AS [ParentId],
[vwUnits].[Order] AS [Order]
FROM [dbo].[vwUnits] AS [vwUnits]) AS [Extent10] ON [Extent1].[UnitCode] = [Extent10].[Id]
LEFT OUTER JOIN (SELECT
[vwCountries].[Id] AS [Id],
[vwCountries].[AttributeGroupId] AS [AttributeGroupId],
[vwCountries].[Title] AS [Title],
[vwCountries].[ParentId] AS [ParentId],
[vwCountries].[Order] AS [Order]
FROM [dbo].[vwCountries] AS [vwCountries]) AS [Extent11] ON [Extent1].[CountryCode] = [Extent11].[Id]
LEFT OUTER JOIN [dbo].[Manufacturers] AS [Extent12] ON [Extent1].[ManufacturerId] = [Extent12].[Id]
LEFT OUTER JOIN (SELECT
[vwProductGroups].[Id] AS [Id],
[vwProductGroups].[AttributeGroupId] AS [AttributeGroupId],
[vwProductGroups].[Title] AS [Title],
[vwProductGroups].[LanguageId] AS [LanguageId],
[vwProductGroups].[ParentId] AS [ParentId],
[vwProductGroups].[Order] AS [Order]
FROM [dbo].[vwProductGroups] AS [vwProductGroups]) AS [Extent13] ON [Extent1].[ProductGroupCode] = [Extent13].[Id]
) AS [Project1]
) AS [Project1]
WHERE [Project1].[row_number] > 11490
ORDER BY [Project1].[Id] ASC',N'@p__linq__0 int,@p__linq__1 int',@p__linq__1=2380,@p__linq__0= 1
```
__but the exactly same direct query runs in less than a second! I replaced "CASE WHEN (@p__linq__0 = 1)" with "CASE WHEN 1 = 1" in the above query and everything works great and fast after that!__
Comments: Sure! : ``` var q = from r in (ProductRepository.AllIncluding(product => product.Manufacturer, product => product.Country, product => product.Unit, product => product.ProductGroup)) select r; .... var q2 = from r in ProductRepository.All join rp in ResourceProductRepository.All on r.Id equals rp.ProductId into g1 from r2 in g1.DefaultIfEmpty() join rr in ResourceRepository.AllRoots on r2.ResourceRootId equals rr.Id into g2 from r3 in g2.DefaultIfEmpty() join r4 in ResourceRepository.All on r3.Id equals r4.ResourceRootId into g3 from resource in g3.DefaultIfEmpty() where resource.ResourceSubGroup.ResourceGroup.ResourceCategory.ResourceType.OrganizationPeriodId == (Commons.CurrentUser.IsActivePeriodLoaded ? Commons.CurrentUser.ActivePeriodId : 0) select new { ProductId = r.Id, ResourceNo = resource.No, ResourceTitle = resource.Title }; return from r in q join r2 in q2 on r.Id equals r2.ProductId into g from resource in g.DefaultIfEmpty() select new ProductGridRowViewModel { Id = r.Id, No = r.No, ShortTitle = r.ShortTitle, UnitTitle = r.Unit.Title, CountryTitle = r.Country.Title, IranCodeNo = r.IranCodeNo, IsService = r.IsService, IsApproved = r.IsApproved, ManufacturerTitle = r.Manufacturer.Title, ProductGroupTitle = r.ProductGroup.Title, RelatedResourceTitle = resource.ResourceTitle, RelatedResourceNo = resource.ResourceNo, }; ``` the problem is with this part : ``` resource.ResourceSubGroup.ResourceGroup.ResourceCategory.ResourceType.OrganizationPeriodId == (Commons.CurrentUser.IsActivePeriodLoaded ? Commons.CurrentUser.ActivePeriodId : 0) ``` The work-around was to check this condition in code and put it into a new variable , so I will have something like this : ``` int activePeriodId = Commons.CurrentUser.IsActivePeriodLoaded ? Commons.CurrentUser.ActivePeriodId : 0;//NOTE! this appraoch(using a variable instead of using it directly in the query) helps EntityFramework to run 30 times faster! (less than a second instead of 15 seconds!!!) //and in where clause : .... where resource.ResourceSubGroup.ResourceGroup.ResourceCategory.ResourceType.OrganizationPeriodId == activePeriodId ... ```