NHibernate 获得下一个生日

发布于 2024-11-17 17:35:06 字数 107 浏览 5 评论 0原文

我有一个表,其中包含日期时间类型的生日列。现在我应该使用 HQL 选择生日在接下来 10 天内或过去 5 天内的所有人员。如何使用 NHibernate 3.2 HQL 做到这一点? 谢谢。 托马斯

I have a table which contains a column Birthday of Type DateTime. Now I should select with HQL all Persons which Birthday is in the next 10 days or was in the last 5 days. How can I do this with NHibernate 3.2 HQL?
Thanks.
Thomas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离旧人 2024-11-24 17:35:06

我已经解决了

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

I have solved it with

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>();
单身狗的梦 2024-11-24 17:35:06

在 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))

In HQL one way is:

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

Alternatively, however i am unsure if the between is inclusive or not off the top of my head:

Session.CreateQuery("FROM PersonTable WHERE Birthday BETWEEN :todayLessFiveDays AND :todayPlusTenDays")
    .SetParameter(":todayPlusTenDays", DateTime.Today.AddDays(10))
    .SetParameter(":todayLessFiveDays", DateTime.Today.AddDays(-5))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文