Skip to content

LINQ "Contains" generates SQL with wrong char length #32325

@carden-jeremy

Description

@carden-jeremy

File a bug

My program worked correctly with EF7, but when I upgraded to EF8,I encountered this problem. When I use Contains(x => x.Property1 + ";" + x.Property2) in a LINQ query, the generated SQL uses a variable length limited to the length of only Property1, so the Contains() always returns zero results.

This also happens with varchar columns, not just char columns.

Include your code

Full demo attached.
snippet from my attached demo:

List<string> queryData = ["AAA;BBBBB", "AAA;CCCCC"];
List<Person> results = dbContext.Persons.Where(x => queryData.Contains(x.ThreeCharacterProperty + ";" + x.FiveCharacterProperty)).ToList();

Generated SQL:

exec sp_executesql N'SELECT [p].[Id], [p].[FiveCharacterProperty], [p].[ThreeCharacterProperty]
FROM [Persons] AS [p]
WHERE [p].[ThreeCharacterProperty] + '';'' + [p].[FiveCharacterProperty] IN (
    SELECT [q].[value]
    FROM OPENJSON(@__queryData_0) WITH ([value] char(3) ''$'') AS [q]
)',N'@__queryData_0 nvarchar(4000)',@__queryData_0=N'["AAA;BBBBB","AAA;CCCCC"]'

// the problem can be fixed by changing char(3) to char(9) because 9 = 3 + 1 + 5

BugRepro1.zip

Include provider and version information

EF Core version: 8.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework:.NET 8.0
Operating system: Windows 10 Pro 22H2
IDE: Visual Studio 2022 17.8.0

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions