In my code, I have the following fragment of an L2E request:
where ol.ordhead.ohcustno == login && (ol.ollastdoctype == "IN") && ol.olstatus == "9"
This means the following SQL fragment:
WHERE ([Extent8].[ohcustno] = @p__linq__1) AND (''IN'' = [Extent7].[ollastdoctype]) AND (''9'' = [Extent7].[olstatus]) ...
At a specific input, the query takes 3 seconds. I change the request like this:
where ol.ordhead.ohcustno == login && (ol.ollastdoctype == "IN" || ol.ollastdoctype == "CR") && ol.olstatus == "9"
and the resulting SQL changes look like this:
WHERE ([Extent6].[ohcustno] = @p__linq__1) AND ([Extent5].[ollastdoctype] IN (N''IN'',N''CR'')) AND (''9'' = [Extent5].[olstatus]) ...
Note that for some bizarre reason, the Entity Framework decided to convert my IN and CR to unicode. As a result, the query now executes 6 seconds on the same input. If you manually remove the prefix N from the IN clause and re-run the request in SSMS, the execution time will return to 3 seconds. This, of course, is because the SQL Server query optimizer cannot use the index, because the types being compared are now different (varchar vs nvarchar)
Can someone explain to me why the Entity Framework suddenly decides to convert my constants to unicode and how can I avoid it?
source share