Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a NotEqual to in NHibernate

Tags:

c#

nhibernate

I have an enumeration of type int in my entity, UserStatus.

I want to get all users where the UserStatus <> Cancelled.

So:

Session.CreateCriteria(typeof(User))
.Add(Expression.Eq("UserStatus", (int)UserStatus.Cancelled)
.UniqueResult<User>();

The above is fore equal, I need to get not equal.

like image 482
Blankman Avatar asked Apr 25 '10 21:04

Blankman


1 Answers

With thanks to Alex use this for NH2 and up:

Session.CreateCriteria(typeof(User))
.Add(Restrictions.Not(Restrictions.Eq("UserStatus", (int)UserStatus.Cancelled))
.UniqueResult<User>();

For older Versions this would be:

Session.CreateCriteria(typeof(User))
.Add(Expression.Not(Expression.Eq("UserStatus", (int)UserStatus.Cancelled))
.UniqueResult<User>();
like image 57
zoidbeck Avatar answered Oct 09 '22 03:10

zoidbeck