Strongly typed properties with NHIbernate

I use NHibernate in my project, but I do not like to use typed properties to select items from the database. Is it possible instead

session.CreateCriteria(typeof(IEntry)).AddOrder(Order.Desc("Alias"))

somthing like this

session.CreateCriteria(typeof(IEntry)).AddOrder(Order.Desc(x=>x.Alias))

Thank you, Alexander.


I tried to use NHibernate.Link, but I can’t use it because it does not have a strong name :( Will wait for the next version and continue to use my solution now

+3
source share
3 answers

With NH 2 you can use nh lambda extensions

list = session.CreateCriteria(typeof(Cat))
    .Add<Cat>( c => c.Age >= 2 && c.Age <= 8 )
    .AddOrder<Cat>( c => c.Name, Order.Desc )
    .List<Cat>();

In NH 3 you should use QueryOver

list = session.QueryOver<Cat>()
    .WhereRestrictionOn(c => c.Age).IsBetween(2).And(8)
    .OrderBy(c => c.Name).Desc
    .List<Cat>();

Or you can use NHibernate.Linq

list = (from c in session.Linq<Cat>()
    where c.Age >= 2 && c.Age <= 8
    orderby c.Name descending
    select c).ToList<Cat>();
+9
source

There are two ways in the NHibernate trunk (version 3.0):

  • , api
  • Linq
+2
+1

Source: https://habr.com/ru/post/1738881/


All Articles