This LINQ expression:
var result = entities.Cases
.Where(c => c.House.Address.Contains("otte"))
.ToList();
Executes this sql at the server:
SELECT
...
--rows
...
FROM [dbo].[Case] AS [Extent1]
INNER JOIN [dbo].[House] AS [Extent2] ON [Extent1].[HouseID_FK] = [Extent2].[HouseID]
WHERE [Extent2].[Address] LIKE '%otte%'
This takes about 100 ms to complete.
This LINQ expression:
var value = "otte";
var result = entities.Cases
.Where(c => c.House.Address.Contains(value))
.ToList();
Executes this sql at the server:
exec sp_executesql N'SELECT
...
--rows
...
FROM [dbo].[Case] AS [Extent1]
INNER JOIN [dbo].[House] AS [Extent2] ON [Extent1].[HouseID_FK] = [Extent2].[HouseID]
WHERE [Extent2].[Address] LIKE @p__linq__0 ESCAPE N''~''',N'@p__linq__0 nvarchar(4000)',@p__linq__0=N'%otte%'
This takes about 1400 ms to complete.
If i declare "value" as a constant i can make it generate the "fast" sql too, but i want to be able to change the value of "value" at runtime. Is there any way to force Entity Framework to not generate "exec sp_executesql" - style sql as this is obviously much slower?