[英]How do I manipulate an NHibernate order by criteria projection?
I've got an NHibernate criterion我有一个 NHibernate 标准
var criteria = GetCurrentSession().CreateCriteria<T>();
to which I add the following order by clauses:我在其中添加了以下 order by 子句:
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))
};
using使用
foreach (var expression in orderExpressions)
{
criteria.AddOrder(expression);
}
Now this works and is equivalent to the following SQL statement:现在这有效并且等效于以下 SQL 语句:
select * from DT
order by order_date desc,
type asc
What I actually need is:我真正需要的是:
select * from DT
order by DATEADD(MINUTE, DATEDIFF(MINUTE, 0, order_date), 0) desc,
type asc
which is essentially ordering it based on the date but ignoring the seconds.这基本上是根据日期对其进行排序,但忽略了秒数。 How do I incorporate this to the above NHibernate criteria expression?
如何将其合并到上述 NHibernate 标准表达式中?
A bit of hunting around lead me to the SQLFunctionTemplate
class for unsupported SQL functions.一些搜索使我找到了
SQLFunctionTemplate
类,用于不受支持的 SQL 函数。 The query I required can be then made using:我需要的查询然后可以使用:
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))
};
where在哪里
new SQLFunctionTemplate(NHibernateUtil.DateTime,
"DateDiff(MINUTE, 0, ?1)"
)
represents DATEDIFF(MINUTE, 0, order_date)
(?1 represents a parameter to be given later)表示
DATEDIFF(MINUTE, 0, order_date)
(?1 表示后面要给出的参数)
and和
new SQLFunctionTemplate(NHibernateUtil.DateTime,
"DateAdd(MINUTE, " +
new SQLFunctionTemplate(NHibernateUtil.DateTime,
"DateDiff(MINUTE, 0, ?1)"
) +
", 0)")
represents the dateadd
with the datediff
.用
datediff
表示dateadd
。 (this bit DATEADD(MINUTE, DATEDIFF(MINUTE, 0, order_date), 0)
). (此位
DATEADD(MINUTE, DATEDIFF(MINUTE, 0, order_date), 0)
)。
This bit lends the parameter (orderdate) for the datediff
above:该位为上面的
datediff
提供参数 (orderdate):
NHibernateUtil.DateTime,
Projections.Property<DocumentTracking>(x => x.OrderDate)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.