我有NHibernate标准
var criteria = GetCurrentSession().CreateCriteria<T>();
我通过子句添加以下顺序:
var orderExpressions = new List<NHibernate.Criterion.Order>
{
NHibernate.Criterion.Order.Desc(Projections.Property<DT>(x => x.OrderDate)),
NHibernate.Criterion.Order.Asc(Projections.Property<DT>(x => x.Type))
};
使用
foreach (var expression in orderExpressions)
{
criteria.AddOrder(expression);
}
现在这可行并且等同于以下SQL语句:
select * from DT
order by order_date desc,
type asc
我真正需要的是:
select * from DT
order by DATEADD(MINUTE, DATEDIFF(MINUTE, 0, order_date), 0) desc,
type asc
基本上是根据日期排序,但忽略秒。如何将其纳入上述NHibernate标准表达式?
答案 0 :(得分:1)
对于不受支持的SQL函数,有点狩猎引导我到SQLFunctionTemplate
类。我需要的查询可以使用:
var orderExpressions = new List<NHibernate.Criterion.Order>
{
NHibernate.Criterion.Order.Desc(
Projections.SqlFunction(
new SQLFunctionTemplate(NHibernateUtil.DateTime,
"DateAdd(MINUTE, " +
new SQLFunctionTemplate(NHibernateUtil.DateTime,
"DateDiff(MINUTE, 0, ?1)"
) +
", 0)"),
NHibernateUtil.DateTime,
Projections.Property<DocumentTracking>(x => x.OrderDate)
)
),
NHibernate.Criterion.Order.Asc(Projections.Property<DocumentTracking>(x => x.Type))
};
其中
new SQLFunctionTemplate(NHibernateUtil.DateTime,
"DateDiff(MINUTE, 0, ?1)"
)
表示DATEDIFF(MINUTE, 0, order_date)
(?1表示稍后给出的参数)
和
new SQLFunctionTemplate(NHibernateUtil.DateTime,
"DateAdd(MINUTE, " +
new SQLFunctionTemplate(NHibernateUtil.DateTime,
"DateDiff(MINUTE, 0, ?1)"
) +
", 0)")
表示dateadd
和datediff
。 (这一位DATEADD(MINUTE, DATEDIFF(MINUTE, 0, order_date), 0)
)。
此位为上面的datediff
提供参数(orderdate):
NHibernateUtil.DateTime,
Projections.Property<DocumentTracking>(x => x.OrderDate)