Skip to content

LINQ uses ternary expressions to generate error SQL #3591

Open
@HmgsLJ

Description

@HmgsLJ

I ran into a puzzle that using LINQ in a query expression under NET8 and NHibernate5.5.2 uses a ternary expression, and the resulting SQL doesn't look right, the code is as follows:

            var query = (from t in session.Query<Test>()
                               select new
                               {
                                   t.Id,
                                   NameSeq = (t.Name == "One" ? 1 : t.Name == "Tow" ? 2 : 0)
                               }).OrderBy(o => o.NameSeq)
                               .ToList();

By normal logic it should generate the sql:

        test0_."Id" as col_0_0_,
        (case 
            when test0_."UserName"=:p0 then :p3 
            else (case 
                when test0_."UserName"=:p1 then :p5 
                else :p6 
            end) 
        end) as col_1_0_ 
    from
        "Test" test0_ 
    order by
        (case 
            when test0_."UserName"=:p0 then :p3 
            else (case 
                when test0_."UserName"=:p1 then :p5 
                else :p6 
            end) 
        end) asc;

But in reality, it is like this:

select
        test0_."Id" as col_0_0_,
        case 
            when test0_."UserName"=:p0 then TRUE 
            else FALSE 
        end as col_1_0_,
        case 
            when test0_."UserName"=:p1 then TRUE 
            else FALSE 
        end as col_2_0_ 
    from
        "Test" test0_ 
    order by
        (case 
            when test0_."UserName"=:p0 then :p3 
            else (case 
                when test0_."UserName"=:p1 then :p5 
                else :p6 
            end) 
        end) asc;

I want to know how to generate SQL correctly because I need deduplication and I can't do that with my current SQL, which is very thankful.

Activity

hazzik

hazzik commented on Jul 29, 2024

@hazzik
Member

under NET8

Does it work correctly with lower versions?

HmgsLJ

HmgsLJ commented on Jul 29, 2024

@HmgsLJ
Author

under NET8

Does it work correctly with lower versions?
Yes, I tried .NET Core 3.1 and got the same error SQL as above..

gliljas

gliljas commented on Jan 2, 2025

@gliljas
Member

It's not wrong per se. NHibernate just decides that it would rather handle the projection client side, but it can't do that with the OrderBy. The best correction would be to detect that the projection is used in the OrderBy and then prefer the server side execution.

added this to the 5.6 milestone on Jan 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @hazzik@gliljas@fredericDelaporte@HmgsLJ

        Issue actions

          LINQ uses ternary expressions to generate error SQL · Issue #3591 · nhibernate/nhibernate-core