views:

1027

answers:

1

Hi, I can't get sum() to return decimal and it always returns int64 truncating the decimals. I have Googled for a whole day but still can't find a real work around. I have a DB table called ProductPurchase with

QtyPurchased(int) and UnitPurchasePrice(money) columns,

these are mapped to a C# POCO object using NHibernate, where QtyPurchase is a int and UnitPurchasePrice is a decimal property.

I have the following HQL query where I want to get the total purchase amount for a given day:

select sum(detail.QtyPurchased * detail.UnitPurchasePrice) from Domain.Entities.ProductPurchase AS detail where datediff("day", detail.PurchaseDate, :trading_date) = 0

Now for what ever reason, the query.UniqueResult always returns an Int64 integer, truncating the decimals, whereas the SQL generated obviously returns the correct number complete with decimals. Can someone shed somelight on how to get this to return decimal?

I have noticed that if I use SQL (i.e. CreateSqlQuery), I can get the decimal back. Is this a bug with Nhibernate?

Thanks heaps

Steven Kuo

+1  A: 

Inverting the order of the factors did it for me: (price * qty) instead of (qty * price).

I guess it must be checking just the first parameter type, please fill a JIRA issue if this workaround worked for you.

Mauricio Scheffer
Awesome, that worked!!! Thank you so much Nhibernate Legend. This was such a hair pulling issue and now I can write proper HQL. Thanks again.
Just posted on JIRA, http://nhjira.koah.net/browse/NH-1734Please vote on it. Thank you.
Thanks a lot! I got the same issue with orderrow.Amount * product.Price. I inverted the properties and it worked. It is really a shame that a most commonly used product like NHibernate has this shortcomings/bugs nowadays.
Patrick Peters