As I can understand from the docs, when one uses string.Concat
from a querying technology, such as LINQ to Entities, this canonical function should be translated to the correct corresponding store function for the provider being used, meaning Concat
for MsSQL
's T-SQL
. But when I'm running the following test code:
var items = (from item in testDB.Items
where list.Contains(string.Concat(item.Id,":"))
select new
{
Hit = string.Concat(item.Id,":"),
Item = item
}).ToList();
the following SQL is being produced:
SELECT
[Extent1].[Id] AS [Id],
CAST( [Extent1].[Id] AS nvarchar(max)) + N':' AS [C1],
FROM [testDB].[Items] AS [Extent1]
WHERE (CAST([Extent1].[Id] AS nvarchar(max)) + N':' -- N.B.
IN (N'1:', N'2:', N'3:')) AND (CAST([Extent1].[Id] AS nvarchar(max)) + N':' IS NOT NULL)
N.B.: +
(Plus operator) with CAST
instead of Concat
is being used.
Obviously I'm doing something wrong, but what? The problem is that the CAST
to NVARCHAR(MAX)
takes enormous amount of time, especially when several fields are being concatenated.
It looks like it cannot be done, since Concat
uses sql_variant
type, which is not defined by SQLProviderManifest
, overloading is not supported so only one function signature can be mapped(names in schema should be unique), so obviously they just made a short-circuit by concatenating using the plus operator with casts when needed, even when I mapped the Concat
in code-first, the generated SQL
was still using it so it became apparent. I think the only way is to utilize the DbSet.SqlQuery
.