by Oliver
17. May 2012 12:48
Today, I was doing some HQL with NHibernate trying to retrieve some object count from the database. Running the following piece of code, unfortunately, would throw an exception:

System.ArgumentException : The value "0" is not of type "System.Int32" and cannot be used in this generic collection.
Parameter name: value
This was quite confusing for me and I tried a few different things which all did not work. Then I went to debug this line and it turns out that the “0” we’re getting back here is of type long! So the correct version of the above code reads:

Easy, once you know :-)
Alternatively, with newer versions of NHibernate ( > 3.0 but I’m not sure exactly) you can use QueryOver and ToRowCountQuery to get an int count:

ToRowCountInt64Query() on the other hand would return a long.
Happy coding!