如何在c#的NHibernate的QueryOver中使用Oracle的内置关键字或函数

本文关键字:内置 Oracle 关键字 函数 NHibernate QueryOver | 更新日期: 2023-09-27 18:07:57

我使用Oracle作为数据库。如何在NHibernate的QueryOver中转换以下hibernate查询。

string condition = "RepositoryItemDO.RepositoryType = :REPTYPE AND 
24*(SYSDATE-RepositoryItemDO.FingerPrint.CreatingDateTime) >= :HOURS"
                    + " AND RepositoryItemDO.TargetEncryption IS NOT NULL AND RepositoryItemDO.TargetStorage IS NOT NULL";

如何在QueryOver中编写上述查询的语法?

如何在c#的NHibernate的QueryOver中使用Oracle的内置关键字或函数

您可以使用VarArgsSQLFunction来使用QueryOver中的算术运算,也可以使用NoArgSQLFunction来访问sysdate, NoArgSQLFunction已经在OracleDilect中定义了。

下面是示例查询,您可以尝试使用queryover

var func = Projections.SqlFunction(new VarArgsSQLFunction(NHibernateUtil.Double, "(", "-", ")"), NHibernateUtil.DateTime, Projections.SqlFunction("sysdate", NHibernateUtil.DateTime), Projections.Property<AdministrativeCaseEO>(c => c.EffectiveCasePeriod.EffectiveStartDate));
var parfunc = Projections.SqlFunction(new VarArgsSQLFunction(NHibernateUtil.Double, "(", "*", ")"), NHibernateUtil.Decimal, func, Projections.Constant(24));
var conjunction = Restrictions.Conjunction()
.Add(Restrictions.Eq(Projections.Property<RepositoryItemDO>(c => c.RepositoryType ), yourRefType))
.Add(Restrictions.IsNotNull(Projections.Property<RepositoryItemDO>(c => c.TargetEncryption)))
.Add(Restrictions.IsNotNull(Projections.Property<RepositoryItemDO>(c => c.TargetStorage)))
.Add(dateConj);