__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: Hello Mahmoud, Glad to hear that you found the workaround. That is probably the best approach to deal with this kind of query currently. I will proceed to close this item, but there a few potential improvements we would like to look at in the future that would make the workaround unnecessary: 1. [More aggressive funcletization (a.k.a. client-side evaluation) of client-side sub-expressions]((https://entityframework.codeplex.com/workitem/1586)): Since Cmmons.CurrentUser is not mapped to an object in the database (at least not for the purpose of this query), EF could automatically produce the same effects you are getting by assigning the results of computing (Commons.CurrentUser.IsActivePeriodLoaded ? Commons.CurrentUser.ActivePeriodId : 0) to a parameter. 2. [Changing the translation of conditional expressions to avoid using CASE statements](https://entityframework.codeplex.com/workitem/1587): In general CASE clauses used in predicates prevent SQL from leveraging indexes. When a value has to be projected or returned there is probably no option than to use a CASE statements but in predicates other alternatives exist, e.g. (@IsActive AND Table.OrganizatonPeriodId = ActivePeriodId) OR (NOT @IsActive AND Table.OrganizationPeridoId = 0). We should look at whether this improves the performance.
```
{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: Hello Mahmoud, Glad to hear that you found the workaround. That is probably the best approach to deal with this kind of query currently. I will proceed to close this item, but there a few potential improvements we would like to look at in the future that would make the workaround unnecessary: 1. [More aggressive funcletization (a.k.a. client-side evaluation) of client-side sub-expressions]((https://entityframework.codeplex.com/workitem/1586)): Since Cmmons.CurrentUser is not mapped to an object in the database (at least not for the purpose of this query), EF could automatically produce the same effects you are getting by assigning the results of computing (Commons.CurrentUser.IsActivePeriodLoaded ? Commons.CurrentUser.ActivePeriodId : 0) to a parameter. 2. [Changing the translation of conditional expressions to avoid using CASE statements](https://entityframework.codeplex.com/workitem/1587): In general CASE clauses used in predicates prevent SQL from leveraging indexes. When a value has to be projected or returned there is probably no option than to use a CASE statements but in predicates other alternatives exist, e.g. (@IsActive AND Table.OrganizatonPeriodId = ActivePeriodId) OR (NOT @IsActive AND Table.OrganizationPeridoId = 0). We should look at whether this improves the performance.