Có tổ chức NHibernate:LINQ to NHibernate: tổng số tiền
Company
, Invoice
và InvoiceLine
trong đó có một tài sản Price
thập phân.
A Company
có bộ sưu tập loại Invoice
và Invoice
có bộ sưu tập loại InvoiceLine
.
Làm cách nào để có được tổng của tất cả các giá thuộc về các dòng hóa đơn thuộc về hóa đơn của một công ty cụ thể được chỉ định bởi id?
Tôi cố gắng để viết các truy vấn như thế này:
session
.Query<InvoiceLine>()
.Where(invoiceLine => invoiceLine.Invoice.Company.Id == companyId)
.Sum(invoiceLine => invoiceLine.Price);
nhưng nó ném một ngoại lệ:
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)
ngoại lệ bên trong:
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)
Điều này có thể có cái gì để làm với tổng hợp các bộ sưu tập trống nhưng tôi không chắc chắn cách khắc phục nó.
Đây gần như là giải pháp, vui lòng thay đổi thành '.Sum (invoiceLine => (decimal?) InvoiceLine.Price) ?? 0' vì vậy tôi có thể upvote và chấp nhận. –