Discussion:
[nhusers] LINQ gerenrating a 'or' when I user '==' operator
Jean Bastos
2018-09-26 14:00:06 UTC
Permalink
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 my 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")]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'

Someone knows why and how solve this?
--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.
Ricardo Peres
2018-09-27 06:31:12 UTC
Permalink
Have you tried setting NotNull = true in the [Property]?
If you think about it, it makes sense: if you want the column to have a value different than X, an option is for it to be null. And because you are using attributes to map your classes, you need to tell NHibernate exactly what you want.

RP
--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.
Jean Bastos
2018-09-27 10:16:55 UTC
Permalink
Yes, I did.
I put NotNull = true in the [Property], but don't makes diference.
Post by Ricardo Peres
Have you tried setting NotNull = true in the [Property]?
If you think about it, it makes sense: if you want the column to have a
value different than X, an option is for it to be null. And because you are
using attributes to map your classes, you need to tell NHibernate exactly
what you want.
RP
--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at https://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.
Ricardo Peres
2018-09-27 13:07:33 UTC
Permalink
OK, so it seems NHibernate is ignoring it.
But, to be fair, other than skipping the null check, the query does make
sense because if you want a value to be different than something, it can
also be null. The syntax for null is different and if you compare something
with null using either = or <>, you always get false.

RP
--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.
Loading...