gpt4 book ai didi

c# - Linq 到 NHibernate : sum of sums

转载 作者:太空狗 更新时间:2023-10-29 21:22:48 24 4
gpt4 key购买 nike

拥有 NHibernate 实体:

CompanyInvoiceInvoiceLine 具有 decimal 属性 Price

Company 有一个 Invoice 类型的集合,一个 Invoice 有一个 InvoiceLine 类型的集合。

如何获取属于id指定的某个公司的发票的发票行的所有价格的总和?

我试过这样写查询:

session
.Query<InvoiceLine>()
.Where(invoiceLine => invoiceLine.Invoice.Company.Id == companyId)
.Sum(invoiceLine => invoiceLine.Price);

但是它抛出一个异常:

NHibernate.Exceptions.GenericADOException
"Could not execute query[SQL: SQL not available]"

at NHibernate.Impl.SessionImpl.List(IQueryExpression queryExpression, QueryParameters queryParameters, IList results)
at NHibernate.Impl.AbstractSessionImpl.List(IQueryExpression queryExpression, QueryParameters parameters)
at NHibernate.Impl.ExpressionQueryImpl.List()
at NHibernate.Linq.DefaultQueryProvider.ExecuteQuery(NhLinqExpression nhLinqExpression, IQuery query, NhLinqExpression nhQuery)
at NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression)
at NHibernate.Linq.DefaultQueryProvider.Execute[TResult](Expression expression)
at System.Linq.Queryable.Sum[TSource](IQueryable`1 source, Expression`1 selector)

内部异常:

System.ArgumentNullException
"Value cannot be null.\r\nParameter name: item"

at System.ThrowHelper.IfNullAndNullsAreIllegalThenThrow[T](Object value, ExceptionArgument argName)
at System.Collections.Generic.List`1.System.Collections.IList.Add(Object item)
at NHibernate.Util.ArrayHelper.<>c__DisplayClass2.<AddAll>b__0()
at NHibernate.Util.ArrayHelper.AddAll(IList to, IList from)
at NHibernate.Engine.Query.HQLQueryPlan.PerformList(QueryParameters queryParameters, ISessionImplementor session, IList results)
at NHibernate.Impl.SessionImpl.List(IQueryExpression queryExpression, QueryParameters queryParameters, IList results)

这可能与对空集合求和有关,但我不确定如何解决。

最佳答案

尝试将 Price 转换为可为 null 的 decimal...

.Sum(invoiceLine => (decimal?)invoiceLine.Price) ?? 0;

结果显然是小数?

关于c# - Linq 到 NHibernate : sum of sums,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18121629/

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