Skip to content

wrong generated query #33839

@vzabavnov

Description

@vzabavnov

Using EF 8 with MS SQL provider.
have this query:

FmSpecimenLocations
.Join(FwSampleLocations, z => new
    {
        Barcode = z.CorrectBarcode != null ? z.CorrectBarcode : Convert.ToInt64(z.Barcode),
        SapID = z.ClientID,
    },
    z => new
    {
        z.Barcode,
        SapID = Convert.ToInt32(z.SapID),
    },
    (l, r) => new
    {
        Fm = l,
        Fw = r
    })

it generates this SQL request:

SELECT [f].[id], [f].[barcode], ...(cut for simpliticy)
FROM [FmSpecimenLocations] AS [f]
INNER JOIN [v_FwSampleLocations] AS [v] ON (CASE
    WHEN [f].[correct_barcode] IS NOT NULL THEN [f].[correct_barcode]
    ELSE CONVERT(bigint, [f].[barcode])
END = [v].[Barcode] OR (CASE
    WHEN [f].[correct_barcode] IS NOT NULL THEN [f].[correct_barcode]
    ELSE CONVERT(bigint, [f].[barcode])
END IS NULL AND [v].[Barcode] IS NULL)) AND

there is a AND at the end of query and it creates an error.

when I modify query by re-ording keys like this:

FmSpecimenLocations
.Join(FwSampleLocations, z => new
    {
        SapID = z.ClientID,
        Barcode = z.CorrectBarcode != null ? z.CorrectBarcode : Convert.ToInt64(z.Barcode),
    },
    z => new
    {
        SapID = Convert.ToInt32(z.SapID),
        z.Barcode,
    },
    (l, r) => new
    {
        Fm = l,
        Fw = r
    })

it generates this SQL request:

SELECT [f].[id], [f].[barcode], ...(cut for simpliticy)
FROM [FmSpecimenLocations] AS [f]
INNER JOIN [v_FwSampleLocations] AS [v] ON CASE
    WHEN [f].[correct_barcode] IS NOT NULL THEN [f].[correct_barcode]
    ELSE CONVERT(bigint, [f].[barcode])
END = [v].[Barcode]

and no issue.
another way I found to avoid that additional AND to replace

SapID = Convert.ToInt32(z.SapID),

by

SapID = (int)z.SapID,

like this:

FmSpecimenLocations
.Join(FwSampleLocations, z => new
    {
        Barcode = z.CorrectBarcode != null ? z.CorrectBarcode : Convert.ToInt64(z.Barcode),
        SapID = z.ClientID,
    },
    z => new
    {
        z.Barcode,
        SapID = (int)z.SapID,
    },
    (l, r) => new
    {
        Fm = l,
        Fw = r
    })

it generates workable SQL request:

SELECT [f].[id], [f].[barcode], ...(cut for simpliticy)
FROM [FmSpecimenLocations] AS [f]
INNER JOIN [v_FwSampleLocations] AS [v] ON (CASE
    WHEN [f].[correct_barcode] IS NOT NULL THEN [f].[correct_barcode]
    ELSE CONVERT(bigint, [f].[barcode])
END = [v].[Barcode] OR (CASE
    WHEN [f].[correct_barcode] IS NOT NULL THEN [f].[correct_barcode]
    ELSE CONVERT(bigint, [f].[barcode])
END IS NULL AND [v].[Barcode] IS NULL)) AND [f].[client_id] = [v].[sap_id]

the type of z.SapID, is int?. it may help to find what is wrong

The DB Context model is very big. if will be necessary I may execute any tests to find out what may cause the issue.

EF Core version: 8.0.6. I found this in version 8.0.3
Database provider: Microsoft.EntityFrameworkCore.SqlServer 8.0.6
Target framework: net8.0
Operating system: Windows 11

Metadata

Metadata

Assignees

Type

No fields configured for Bug.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions