0

我正在研究 NHibernate 标准,我根据输入参数逐渐建立 upp。

我对这些参数的邮政部分有一些问题。由于我们得到了一个 5 位数的邮政编码,因此输入参数是一个 int,但由于我们在数据库中也接受外国邮政编码,因此数据库将其保存为字符串。

我试图在 NHibernate Criteria/Criterion 中复制的是以下 where 子句。

WHERE
11182 <=
    (case when this_.SendInformation = 0 AND dbo.IsInteger(this_.Zipcode) = 1 then
        CAST(REPLACE(this_.Zipcode, ' ', '') AS int)
    when this_.SendInformation = 1 AND dbo.IsInteger(this_.WorkZipcode) = 1 then
        CAST(REPLACE(this_.WorkZipcode, ' ', '') AS int)
    when this_.SendInformation = 2 AND dbo.IsInteger(this_.InvoiceZipcode) = 1 then
        CAST(REPLACE(this_.InvoiceZipcode, ' ', '') AS int)
    else
        NULL
    end)

我们所做的是检查成员this_联系人IsInteger(expr)(将一侧标记为NULL

在这种情况下,我们只检查邮政编码是否 >= 输入参数(在 sql 代码中反转,因为参数是第一个),目标是做一个 between(2 个用“AND”语句包裹的子句),>= 或 <=。

更新

得到了成功的暗示。

Projections.SqlProjection("(CASE when SendInformation = 0 AND dbo.IsInteger(Zipcode) = 1 then CAST(REPLACE(Zipcode, ' ', '') AS int) when SendInformation = 1 AND dbo.IsInteger(WorkZipcode) = 1 then CAST(REPLACE(WorkZipcode, ' ', '') AS int) when SendInformation = 2 AND dbo.IsInteger(InvoiceZipcode) = 1 then CAST(REPLACE(InvoiceZipcode, ' ', '') AS int) else NULL END)"
                , new[] { "SendInformation", "Zipcode", "WorkZipcode", "InvoiceZipcode" },
                new[] { NHibernateUtil.Int32, NHibernateUtil.String, NHibernateUtil.String, NHibernateUtil.String });

将我的整个子句放在 Projections.SqlProjection 中,但是当我运行我的代码时,我的一些投影被剪切(“AS int)否则 NULL END)”从最后被剪切)并使 sql 损坏。对此有某种限制吗?

4

1 回答 1

0

昨天开始工作了。

Projections.SqlProjection 有效,但是如果您不将投影命名为列,那么它会如何削减一些 TSQL 代码。

(Case
    when x = 1 then 'bla'
    when x = 2 then 'bla_bla'
    else NULL
 END) as foo

当使用最后一部分 ( as foo) 并命名整个 case 语法时,它可以工作并且不会削减任何内容。

但是我不知道为什么,但我无法使用标准其他部分的别名。

于 2012-10-02T06:59:50.827 回答