【问题标题】:Nhibernate Criteria Conditional WhereNhibernate 条件条件 Where
【发布时间】:2012-10-01 07:56:39
【问题描述】:

我正在研究 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_) 首选将信息发送到的位置,然后我们根据列是否可转换为 int (@987654323) 将输入的邮政编码作为整数与三个不同的列进行对比@函数)如果列不可转换,我们将边标记为NULL

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

更新

有一点成功的迹象。

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 损坏。 这有什么限制吗?

【问题讨论】:

  • 尽管我不是 T-SQL 的粉丝,但这可能是使用 SQL 函数封装逻辑然后使用公式映射属性或使用sqlprojection,但调用函数(我不知道 sql 投影中 sql 长度的限制)

标签: sql nhibernate case criteria where


【解决方案1】:

昨天搞定了。

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    • 1970-01-01
    • 2012-12-06
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多