Skip to content

LINQ "==" operator generates OR with IS NULL #1860

Closed
@jeanrbastos

Description

@jeanrbastos

When I do this query:

var query = from entity in session.Query<Entity>()
            where entity.Status != 2
            select entity;

NHibernate generate this SQL:

SELECT ...
FROM TABLE_NAME ALIAS
WHERE ALIAS.SITFIL <> 2 
       or ALIAS.SITFIL is null /*this 'or' is the problem*/

Why NHibernate do this?
The 'or' causes a performance issue on application I want to generate just:

SELECT ...
FROM TABLE_NAME ALIAS
WHERE ALIAS.SITFIL <> 2 

Without 'Or field is null'
The mapping not is nullable:

[Property(Column = "SITFIL", NotNull = true)]
public virtual TypeEnumOfProperty Status { get; set; }

The enum:

public enum TypeEnumOfProperty
{
    [Display(Name = "Ativa", Description ="Ativo")]
    Ativa = 0,
    [Display(Name = "Inativa", Description = "Inativo")]
    Inativa = 1,
    [Display(Name = "Inativa e oculta", Description = "Inativo e oculto")]
    InativaOculta = 2,
}

If I change the TypeEnumOfProperty to int the SQL is generated like I want, if change to TypeEnumOfProperty or long then was generated with the 'or'.

If I use 'equals' on comparision, NHibernate generates the where clause without 'or'.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions