December 7, 2009 NHibernate
December 7, 2009 NHibernate
Image that you want to fetch friends from database by the First Name, Last Name and Age, which are properties of your class Friend. With NHibernate you could write query which will look like:
return friends;
}
But it is wrong. Since Age is the Nullable type (int?), your call GetFriends(“Andriy”,”Buday”,null) will not get my record even if there is such in database and the column AGE is NULL there. So to request you need this: ageExpression = Expression.IsNull(“Age”); but it will not work for not null age.
For our luck there is AbstractCriterion so we can generalize our Expressions like here:
IList<Friend> friends = Session.CreateCriteria(typeof(Friend))
.Add(Expression.Eq(“FirstName”, firstName))
.Add(Expression.Eq(“LastName”, lastName))
.Add(ageExpression)
.List<Friend>();
return friends;
}
Markdown | Result |
---|---|
*text* | text |
**text** | text |
***text*** | text |
`code` | code |
~~~ more code ~~~~ |
more code |
[Link](https://www.example.com) | Link |
* Listitem |
|
> Quote | Quote |
Thanks for the post, it helped me.
Great.