NHibernate Criteria Restriction vs Expression

Darius Kucinskas picture Darius Kucinskas · Mar 30, 2011 · Viewed 14.8k times · Source

If I search for NHibernate Criteria API query examples in internet there are examples that use Restrictions and others use Expression. What are the differences between those two?

For example:

posts = session.CreateCriteria<Post>()
    .Add(Expression.Eq("Id", 1))
    .List<Post>();

posts = session.CreateCriteria<Post>()
    .Add(Restrictions.Eq("Id", 1))
    .List<Post>();

Answer

Rippo picture Rippo · Mar 30, 2011

I think Restrictions were released in NH2 and is now the favoured way.

According to Resharper whenever I use Expression I get a hint to say Access to a static member of a type via a derived type

Also according to this post by Ayende:-

Prefer to use the Restrictions instead of the Expression class for defining Criteria queries.