gpt4 book ai didi

nhibernate - 在字符串属性上使用不等式搜索

转载 作者:行者123 更新时间:2023-12-04 09:46:14 24 4
gpt4 key购买 nike

我们在 SQL Server 和 SQLite 上使用 NHibernate。
数据库将记录存储在行而不是列中——每行都有 Path|Value作为列。路径和值都是 string特性。

对于 Path 的某些值我们想查询不等式——大于、小于等。

我们遇到的麻烦是因为属性是字符串,所以不等式使用字符串比较——例如,搜索 Value >= 18返回行,其中 Value = 5 .不幸的是,我们在解决这个问题时遇到了麻烦。

1) 此限制产生不正确的结果(如 18 < 5 ):
Restrictions.Ge("Value", item.Value);
2) 我们尝试将值转换为整数,但此代码生成 SqlException来自 NHibernate -- Error converting data type nvarchar to bigint.Restrictions.Le(Projections.Cast(NHibernateUtil.Int64, Projections.Property("SearchString")), item.Value)
3) 我们正在寻找一种用零填充属性值的方法(以便我们得到 018 > 005 ),但在 NHibernate 中找不到这样做的方法。

有人有建议吗?
先感谢您!

最佳答案

假设您要比较整数值,使用 IQueryOver :

1) This restriction produces incorrect results (saying 18 < 5):

Restrictions.Ge("Value", item.Value);


Restrictions.Ge
(
Projections.Cast(NHibernateUtil.Int32, Projections.Property<YourEntity>(x => x.YourProperty))
, intValue
)

相应地转换您的数据类型。如果您的 C# 数据类型 ( intValue ) 已经是数字,则无需对其进行转换。如果您的 x.YourProperty已经是数字类型了,不需要转换。相应地调整上面的代码。

2) We tried casting the value to an integer, but this code produces a SqlException from NHibernate -- Error converting data type nvarchar to bigint.

Restrictions.Le(Projections.Cast(NHibernateUtil.Int64, Projections.Property("SearchString")), item.Value)



引用上面并检查 item.Value的数据类型.

关于nhibernate - 在字符串属性上使用不等式搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62096106/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com