NHibernate下一个生日

时间:2011-06-28 20:04:30

标签: c# nhibernate select hql nhibernate-3

我有一个表,其中包含DateTime类型的生日列。现在我应该选择HQL所有人,生日是在接下来的10天或过去5天。我怎么能用NHibernate 3.2 HQL做到这一点? 谢谢。 托马斯

2 个答案:

答案 0 :(得分:2)

我用

解决了这个问题
var result =
session.CreateQuery(@"from Person 
                      where 1 = (FLOOR(DATEDIFF(dd,Birthday,GETDATE()+10) / 365.25))
                                    -
                                (FLOOR(DATEDIFF(dd,Birthday,GETDATE()-5) / 365.25))")
       .List<Person>();

答案 1 :(得分:0)

在HQL中,一种方法是:

Session.CreateQuery("FROM PersonTable WHERE Birthday <= :todayPlusTenDays AND Birthday >= :todayLessFiveDays")
.SetParameter(":todayPlusTenDays", DateTime.Today.AddDays(10))
.SetParameter(":todayLessFiveDays", DateTime.Today.AddDays(-5))

或者,不过我不确定两者之间是否具有包容性或不是我的头脑:

Session.CreateQuery("FROM PersonTable WHERE Birthday BETWEEN :todayLessFiveDays AND :todayPlusTenDays")
    .SetParameter(":todayPlusTenDays", DateTime.Today.AddDays(10))
    .SetParameter(":todayLessFiveDays", DateTime.Today.AddDays(-5))