开发者

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


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


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

  • Query over, the new criteria api example
  • Linq


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜