Search code examples
nhibernatehql

How can I sum a boolean column in HQL (NHibernate)?


I have a column represented as integers in the backend SQLite but with a boolean interpretation in the Nhibernate mapping. I want my HQL query to get a count of the 'true' rows. In SQL it works fine as "SUM(BoolColumn)" but HQL turns the returned value into a bool. I can't use a where clause because I've got some other COUNT(DISTINCT) columns in the select which include both true and false rows.


Solution

  • Since NH thinks it's a bit (which can't be aggregated) you might be able to do something like this in HQL...

    sum(case when BoolColumn = 1 then 1 else 0 end)

    Did you define a custom mapping from int to bool or are you assuming the int will only be 1 or 0?