通过 nHibernate Criterion 使用 SQL CONVERT 函数 [英] Using SQL CONVERT function through nHibernate Criterion
问题描述
我有一个 sql 视图,其中每一列的数据类型都是 string
,用于 jquery 数据表插件.
I have a sql view, in which the data type of every column is string
, for use with the jquery datatables plugin.
其中一列包含英国格式日期 dd/mm/yyyy.
One of the columns contains a uk format date dd/mm/yyyy.
此列需要保留一个字符串才能与插件的击键过滤一起使用,但是为了排序需要将其视为日期.
This column needs to remain a string to work with the keystroke filtering of the plugin, however for sorting it needs to be treated as a date.
我正在使用 nhibernate 标准来创建 sql 查询,并且我想生成以下 order by 子句,因为这可以正确地对日期进行排序
I am using nhibernate criteria to create the sql query and I want to generate the following order by clause, as this orders the dates correctly
order by CONVERT (datetime, DateOfBirth, 103)
但是在 Nhibernate.Criterion.Projections
中没有 Convert
方法.有 Cast
,但由于它是英国日期格式,我收到以下错误:
However in Nhibernate.Criterion.Projections
there is no Convert
method. There is Cast
, but I get the following error due to the fact that it is a UK date format:
The conversion of a varchar data type to a datetime data type
resulted in an out-of-range value
我也尝试过以下方法:
criteria.AddOrder(
Order.Desc(
Projections.SqlFunction(
"CONVERT",
NHibernateUtil.DateTime,
new IProjection[]
{
Projections.Property(propNames[orderByColumn]),
Projections.Property("104")
}
)
)
);
但我收到以下错误消息:
But I get the following error message:
NHibernate.HibernateException: Current dialect
NHibernate.Dialect.MsSql2008Dialect doesn't support the function: CONVERT
使用Nhibernate.Criterion
时能否使用SQL转换函数?
Can the SQL convert function be used when using Nhibernate.Criterion
?
推荐答案
如果设置 104 不是必需的,我们可以得到一个快速的解决方案:使用 CAST 而不是 CONVERT.这个 SQL 函数是内置在 NHibernate 方言中的:
If the setting 104 is not essential, we can get a quick solution: use CAST instead of CONVERT. This SQL Function is built-in in the NHibernate dialects:
Projections.Cast(NHibernateUtil.DateTime
,Projections.Property(propNames[orderByColumn]))
如果设置104很重要,我们可以创建自己的方言,注册 CONVERT 函数,然后使用它......永远
If the setting 104 is important we can create our own Dialect, register the CONVERT function, and use it from then ... forever
这里 Andrew Whitaker 很好地展示了如何
Here Andrew Whitaker nicely shows how to
这篇关于通过 nHibernate Criterion 使用 SQL CONVERT 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!