c# - Linq to NHibernate: sum of sums -


having nhibernate entities:

company, invoice , invoiceline has decimal property price.

a company has collection of type invoice, , invoice has collection of type invoiceline.

how can obtain sum of prices belong invoice lines belong invoices of company specified id?

i tried write query this:

session     .query<invoiceline>()     .where(invoiceline => invoiceline.invoice.company.id == companyid)     .sum(invoiceline => invoiceline.price); 

but throws exception:

nhibernate.exceptions.genericadoexception "could not execute query[sql: sql not available]"     @ nhibernate.impl.sessionimpl.list(iqueryexpression queryexpression, queryparameters queryparameters, ilist results)    @ nhibernate.impl.abstractsessionimpl.list(iqueryexpression queryexpression, queryparameters parameters)    @ nhibernate.impl.expressionqueryimpl.list()    @ nhibernate.linq.defaultqueryprovider.executequery(nhlinqexpression nhlinqexpression, iquery query, nhlinqexpression nhquery)    @ nhibernate.linq.defaultqueryprovider.execute(expression expression)    @ nhibernate.linq.defaultqueryprovider.execute[tresult](expression expression)    @ system.linq.queryable.sum[tsource](iqueryable`1 source, expression`1 selector) 

inner exception:

system.argumentnullexception "value cannot null.\r\nparameter name: item"     @ system.throwhelper.ifnullandnullsareillegalthenthrow[t](object value, exceptionargument argname)    @ system.collections.generic.list`1.system.collections.ilist.add(object item)    @ nhibernate.util.arrayhelper.<>c__displayclass2.<addall>b__0()    @ nhibernate.util.arrayhelper.addall(ilist to, ilist from)    @ nhibernate.engine.query.hqlqueryplan.performlist(queryparameters queryparameters, isessionimplementor session, ilist results)    @ nhibernate.impl.sessionimpl.list(iqueryexpression queryexpression, queryparameters queryparameters, ilist results) 

this might have summing empty collections i'm not sure how fix it.

try casting price nullable decimal...

.sum(invoiceline => (decimal?)invoiceline.price) ?? 0; 

the result decimal?


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -