让我说我有一个列(日期时间),其中包含以下值:
2015-05-16 20:17:05.033
2015-05-16 20:29:07.130
2015-05-17 01:01:04.690
2015-05-17 01:02:28.053
2015-05-17 11:24:37.667
2015-05-17 11:25:24.913
如何选择DateTime list
至
2015-05-16
2015-05-17
在纯SQL中,我可以DISTINCT CONVERT(date, myDateColumn)
但是如何在Fluent nHibernate中执行此操作?
答案 0 :(得分:1)
distinct
要求使这有点复杂,但NHibernate已经注册了各种日期/时间方法,以便在Criteria和QueryOver查询中使用。这与您要求的SQL略有不同,但结果应该相同:
var distinctDates = session.QueryOver<MyTable>()
.Select(Projections.Distinct(
Projections.SqlFunction("date", NHibernateUtil.Date,
Projections.Property<MyTable>(mt => mt.DateCreated))))
.List<DateTime>();
这将生成以下SQL:
SELECT
distinct dateadd(dd, 0, datediff(dd, 0, this_.DateCreated)) as y0_
FROM
MyTable this_
如果没有distinct
要求,您可以执行以下操作:
session.QueryOver<MyTable>()
.Select(mt => mt.DateCreated.Date)
.List<DateTime>()
.Dump();
在QueryOver表达式中,NHibernate&#34;知道&#34;如何将Date
属性转换为正确的SQL。