tags:

views:

155

answers:

5

Hello,

I am using NHibernate in my project, but I dont like to use typed properties for selecting items from database. Is it possible to have instead of

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

somthing like this

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

Thanks, Alexander.

+1  A: 

Here you are: Strongly typed NHibernate Criteria with C# 3

Giorgi
+1  A: 

In the NHibernate trunk (3.0 version) are two ways:

  • Query over, the new criteria api example
  • Linq
Paco
A: 

Thanks, for your help. But your solutin is not good enough for me. I created my own solution, which is fine for me.

I created class to return name of the property using strongly type.

Alexander Shapovalov
-1 for NIH. There already are well-tested, feature-complete solutions.
Mauricio Scheffer
+4  A: 

With NH 2 you could use the 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 would use QueryOver

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

Or you could 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>();
Lachlan Roche
Thanks for introducing me to the nh lambda extensions project!
jwarzech
A: 

I tried to use NHibernate.Link, but I can't use because it has no strong name :( Will wait for next version and continue to use my solution now

Alexander Shapovalov