通过NHibernate的标准使用SQL CONVERT函数 [英] Using SQL CONVERT function through nHibernate Criterion

查看:334
本文介绍了通过NHibernate的标准使用SQL CONVERT函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SQL视图,其中每一列的数据类型为字符串,与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查询,我要生成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 没有转换方法。有铸造,但我得到了下面的错误由于这样的事实,这是一个英国的日期格式为:

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

能否在SQL转换函数中使用 Nhibernate.Criterion

Can the SQL convert function be used when using Nhibernate.Criterion ?

推荐答案

如果设置的 104 不是必需的,我们可以得到一个快速的解决方案:使用CAST而不是进行转换。此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 的是非常重要的我们可以创造我们自己的方言,注册转换功能,并然后用它永远......

If the setting 104 is important we can create our own Dialect, register the CONVERT function, and use it from then ... forever

下面安德鲁·惠特克很好地说明了如何

Here Andrew Whitaker nicely shows how to

  • Nhibernate count distinct (based on multiple columns)

这篇关于通过NHibernate的标准使用SQL CONVERT函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆