How to query using Criteria API using "Not Equal", nHibernate appears to be missing .ne from Hibernate criteria API

3,414 views
Skip to first unread message

MattO

unread,
Aug 25, 2010, 1:56:25 PM8/25/10
to nhusers
This one is throwing me for a bit of a loop and still don't know how
to write a "not equals" query.

In Hibernate for java there appears to be an Expression.ne (not
equals) that you can use. This doesn't exist in nHibernate 2.1.2.

So I tried this:

criteria.Add(!Expression.Eq("ChildFormID",1));

and also this:

criteria.Add(Expression.Not(Expression.Eq("ChildFormID",1)));

But neither of those have the desired effect of writing out my WHERE
SQL clause as follows:

WHERE ChildFormID <> 1

Instead what I get with the above two examples (but it is incorrect
for what I'm trying to accomplish) is

WHERE NOT (ChildFormID = 1)

So how do you do a "Not Equals" using Criterion API??

Tuna Toksoz

unread,
Aug 25, 2010, 2:02:37 PM8/25/10
to nhu...@googlegroups.com
Write your own?

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.


MattO

unread,
Aug 25, 2010, 2:06:21 PM8/25/10
to nhusers
Please clarify? In order to do simple "not equals" comparisons I'll
have to resort to using CreateSQLQuery??

On Aug 25, 1:02 pm, Tuna Toksoz <tehl...@gmail.com> wrote:
> Write your own?
>
> Tuna Toksöz
> Eternal sunshine of the open source mind.
>
> http://devlicio.us/blogs/tuna_toksozhttp://tunatoksoz.comhttp://twitter.com/tehlike
>
>
>
> On Wed, Aug 25, 2010 at 1:56 PM, MattO <xant...@gmail.com> wrote:
> > This one is throwing me for a bit of a loop and still don't know how
> > to write a "not equals" query.
>
> > In Hibernate for java there appears to be an Expression.ne (not
> > equals) that you can use.  This doesn't exist in nHibernate 2.1.2.
>
> > So I tried this:
>
> > criteria.Add(!Expression.Eq("ChildFormID",1));
>
> > and also this:
>
> > criteria.Add(Expression.Not(Expression.Eq("ChildFormID",1)));
>
> > But neither of those have the desired effect of writing out my WHERE
> > SQL clause as follows:
>
> > WHERE ChildFormID <> 1
>
> > Instead what I get with the above two examples (but it is incorrect
> > for what I'm trying to accomplish) is
>
> > WHERE NOT (ChildFormID = 1)
>
> > So how do you do a "Not Equals" using Criterion API??
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "nhusers" group.
> > To post to this group, send email to nhu...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com­>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/nhusers?hl=en.- Hide quoted text -
>
> - Show quoted text -

John Davidson

unread,
Aug 25, 2010, 2:06:27 PM8/25/10
to nhu...@googlegroups.com
Criteria.Add(Criterion.Restrictions.Not(Criterion.Restrictions.Eq(<PropertyName>, Value)))

John Davidson

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.

MattO

unread,
Aug 25, 2010, 2:08:41 PM8/25/10
to nhusers
Yeah, it was actually working. I'm being goofy, I added the wrong
restriction value in so I wasn't seeing the results I wanted.

Both of the examples I provided:

criteria.Add(!Expression.Eq("ChildFormID",1));

and

criteria.Add(Expression.Not(Expression.Eq("ChildFormID",1)));

Work just fine.

On Aug 25, 1:06 pm, John Davidson <jwdavid...@gmail.com> wrote:
> Criteria.Add(Criterion.Restrictions.Not(Criterion.Restrictions.Eq(<Property­Name>,
> Value)))
>
> John Davidson
>
>
>
> On Wed, Aug 25, 2010 at 1:56 PM, MattO <xant...@gmail.com> wrote:
> > This one is throwing me for a bit of a loop and still don't know how
> > to write a "not equals" query.
>
> > In Hibernate for java there appears to be an Expression.ne (not
> > equals) that you can use.  This doesn't exist in nHibernate 2.1.2.
>
> > So I tried this:
>
> > criteria.Add(!Expression.Eq("ChildFormID",1));
>
> > and also this:
>
> > criteria.Add(Expression.Not(Expression.Eq("ChildFormID",1)));
>
> > But neither of those have the desired effect of writing out my WHERE
> > SQL clause as follows:
>
> > WHERE ChildFormID <> 1
>
> > Instead what I get with the above two examples (but it is incorrect
> > for what I'm trying to accomplish) is
>
> > WHERE NOT (ChildFormID = 1)
>
> > So how do you do a "Not Equals" using Criterion API??
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "nhusers" group.
> > To post to this group, send email to nhu...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com­>
> > .
> > For more options, visit this group at
Reply all
Reply to author
Forward
0 new messages