Fluent nHibernate - 如何 select 没有时间的 DISTINCT 日期?
Fluent nHibernate - how to select DISTINCT dates without time?
假设我有一列(日期时间)具有以下值:
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
我怎样才能 select 不同的列表到 DateTime list
到
2015-05-16
2015-05-17
?
在简单的 SQL 中,我可以做到 DISTINCT CONVERT(date, myDateColumn)
但我如何在 Fluent nHibernate 中做到这一点?
distinct
要求使这有点复杂,但 NHibernate 已经注册了各种 date/time 方法用于 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 "knows" 如何将访问 Date
属性 转换为正确的 SQL.
假设我有一列(日期时间)具有以下值:
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
我怎样才能 select 不同的列表到 DateTime list
到
2015-05-16
2015-05-17
?
在简单的 SQL 中,我可以做到 DISTINCT CONVERT(date, myDateColumn)
但我如何在 Fluent nHibernate 中做到这一点?
distinct
要求使这有点复杂,但 NHibernate 已经注册了各种 date/time 方法用于 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 "knows" 如何将访问 Date
属性 转换为正确的 SQL.