NHibernate Criteria Query - 分离查询和子查询:System.FormatException:输入字符串格式不正确
NHibernate Criteria Query - Detached query and subquery: System.FormatException: Input string was not in a correct format
我正在学习 NHibernate 条件查询,我遇到了一个问题希望你们能帮助我。
我有这个型号:
public class Cat
{
public virtual string Id { get; set; } = string.Empty;
public virtual string Name { get; set; } = string.Empty;
public virtual char Sex { get; set; }
public virtual float Weight { get; set; }
public virtual CatStore? CatStore { get; set; }
public virtual DateTime Version { get; set; }
}
然后我 运行 这个查询但是它在第 var cats...
行导致异常:
var avgWeight = DetachedCriteria
.For<Cat>()
.SetProjection(Projections.Avg("Weight"));
using var session = NHibernateHelper.OpenSession();
var cats = session
.CreateCriteria<Cat>()
.Add(Subqueries.Gt("Weight", avgWeight))
.List();
异常:
Message:
NHibernate.Exceptions.GenericADOException : could not execute query
[ SELECT this_.CatId as catid1_3_0_, this_.Version as version2_3_0_, this_.Name as name3_3_0_, this_.Sex as sex4_3_0_, this_.Weight as weight5_3_0_, this_.CatStoreId as catstoreid6_3_0_ FROM Cat this_ WHERE ? > (SELECT avg(cast(this_0_.Weight as FLOAT(53))) as y0_ FROM Cat this_0_) ]
Name:cp0 - Value:Weight
[SQL: SELECT this_.CatId as catid1_3_0_, this_.Version as version2_3_0_, this_.Name as name3_3_0_, this_.Sex as sex4_3_0_, this_.Weight as weight5_3_0_, this_.CatStoreId as catstoreid6_3_0_ FROM Cat this_ WHERE ? > (SELECT avg(cast(this_0_.Weight as FLOAT(53))) as y0_ FROM Cat this_0_)]
----> System.FormatException : Input string was not in a correct format.
Stack Trace:
Loader.DoList(ISessionImplementor session, QueryParameters queryParameters, IResultTransformer forcedResultTransformer, QueryCacheResultBuilder queryCacheResultBuilder)
CriteriaLoaderExtensions.LoadAllToList[T](IList`1 loaders, ISessionImplementor session)
SessionImpl.List[T](CriteriaImpl criteria)
CriteriaImpl.List[T]()
CriteriaImpl.List()
CatStoreTests.DetachQueryAsSubqueryTest() line 284
--FormatException
Number.ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type)
IConvertible.ToDouble(IFormatProvider provider)
Convert.ToDouble(Object value)
DoubleType.Set(DbCommand st, Object value, Int32 index, ISessionImplementor session)
NullableType.NullSafeSet(DbCommand st, Object value, Int32 index, ISessionImplementor session)
CriteriaNamedParameterSpecification.Bind(DbCommand command, IList`1 multiSqlQueryParametersList, Int32 singleSqlParametersOffset, IList`1 sqlQueryParametersList, QueryParameters queryParameters, ISessionImplementor session)
CriteriaNamedParameterSpecification.Bind(DbCommand command, IList`1 sqlQueryParametersList, QueryParameters queryParameters, ISessionImplementor session)
SqlCommandImpl.Bind(DbCommand command, ISessionImplementor session)
Loader.PrepareQueryCommand(QueryParameters queryParameters, Boolean scroll, ISessionImplementor session)
Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies, IResultTransformer forcedResultTransformer, QueryCacheResultBuilder queryCacheResultBuilder)
Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies, IResultTransformer forcedResultTransformer, QueryCacheResultBuilder queryCacheResultBuilder)
Loader.DoList(ISessionImplementor session, QueryParameters queryParameters, IResultTransformer forcedResultTransformer, QueryCacheResultBuilder queryCacheResultBuilder)
我复制异常中生成的query,将?替换为Weight 可以对数据库成功执行。
你们能告诉我这里哪里错了吗?
Subqueries.Gt
用于比较子查询结果与参数值如Subqueries.Gt(11.5, avgSubquery)
。尝试将 Weight
字符串转换为双参数值时抛出异常。
您需要使用 Subqueries.PropertyGt
来代替:
.Add(Subqueries.PropertyGt("Weight", avgWeight))
我正在学习 NHibernate 条件查询,我遇到了一个问题希望你们能帮助我。
我有这个型号:
public class Cat
{
public virtual string Id { get; set; } = string.Empty;
public virtual string Name { get; set; } = string.Empty;
public virtual char Sex { get; set; }
public virtual float Weight { get; set; }
public virtual CatStore? CatStore { get; set; }
public virtual DateTime Version { get; set; }
}
然后我 运行 这个查询但是它在第 var cats...
行导致异常:
var avgWeight = DetachedCriteria
.For<Cat>()
.SetProjection(Projections.Avg("Weight"));
using var session = NHibernateHelper.OpenSession();
var cats = session
.CreateCriteria<Cat>()
.Add(Subqueries.Gt("Weight", avgWeight))
.List();
异常:
Message:
NHibernate.Exceptions.GenericADOException : could not execute query
[ SELECT this_.CatId as catid1_3_0_, this_.Version as version2_3_0_, this_.Name as name3_3_0_, this_.Sex as sex4_3_0_, this_.Weight as weight5_3_0_, this_.CatStoreId as catstoreid6_3_0_ FROM Cat this_ WHERE ? > (SELECT avg(cast(this_0_.Weight as FLOAT(53))) as y0_ FROM Cat this_0_) ]
Name:cp0 - Value:Weight
[SQL: SELECT this_.CatId as catid1_3_0_, this_.Version as version2_3_0_, this_.Name as name3_3_0_, this_.Sex as sex4_3_0_, this_.Weight as weight5_3_0_, this_.CatStoreId as catstoreid6_3_0_ FROM Cat this_ WHERE ? > (SELECT avg(cast(this_0_.Weight as FLOAT(53))) as y0_ FROM Cat this_0_)]
----> System.FormatException : Input string was not in a correct format.
Stack Trace:
Loader.DoList(ISessionImplementor session, QueryParameters queryParameters, IResultTransformer forcedResultTransformer, QueryCacheResultBuilder queryCacheResultBuilder)
CriteriaLoaderExtensions.LoadAllToList[T](IList`1 loaders, ISessionImplementor session)
SessionImpl.List[T](CriteriaImpl criteria)
CriteriaImpl.List[T]()
CriteriaImpl.List()
CatStoreTests.DetachQueryAsSubqueryTest() line 284
--FormatException
Number.ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type)
IConvertible.ToDouble(IFormatProvider provider)
Convert.ToDouble(Object value)
DoubleType.Set(DbCommand st, Object value, Int32 index, ISessionImplementor session)
NullableType.NullSafeSet(DbCommand st, Object value, Int32 index, ISessionImplementor session)
CriteriaNamedParameterSpecification.Bind(DbCommand command, IList`1 multiSqlQueryParametersList, Int32 singleSqlParametersOffset, IList`1 sqlQueryParametersList, QueryParameters queryParameters, ISessionImplementor session)
CriteriaNamedParameterSpecification.Bind(DbCommand command, IList`1 sqlQueryParametersList, QueryParameters queryParameters, ISessionImplementor session)
SqlCommandImpl.Bind(DbCommand command, ISessionImplementor session)
Loader.PrepareQueryCommand(QueryParameters queryParameters, Boolean scroll, ISessionImplementor session)
Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies, IResultTransformer forcedResultTransformer, QueryCacheResultBuilder queryCacheResultBuilder)
Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies, IResultTransformer forcedResultTransformer, QueryCacheResultBuilder queryCacheResultBuilder)
Loader.DoList(ISessionImplementor session, QueryParameters queryParameters, IResultTransformer forcedResultTransformer, QueryCacheResultBuilder queryCacheResultBuilder)
我复制异常中生成的query,将?替换为Weight 可以对数据库成功执行。
你们能告诉我这里哪里错了吗?
Subqueries.Gt
用于比较子查询结果与参数值如Subqueries.Gt(11.5, avgSubquery)
。尝试将 Weight
字符串转换为双参数值时抛出异常。
您需要使用 Subqueries.PropertyGt
来代替:
.Add(Subqueries.PropertyGt("Weight", avgWeight))