Yaplex
Yaplex

Reputation: 2284

Strongly typed properties with NHIbernate

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.


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

Upvotes: 3

Views: 1449

Answers (3)

Lachlan Roche
Lachlan Roche

Reputation: 25946

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>();

Upvotes: 9

Paco
Paco

Reputation: 8381

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

  • Query over, the new criteria api example
  • Linq

Upvotes: 2

Giorgi
Giorgi

Reputation: 30873

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

Upvotes: 1

Related Questions