NHibernate: count(…) will return a long, even if it is 0

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:

image

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:

image

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:

image

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

Happy coding!

enjoyed the post?

Tags:

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

About Oliver

shades-of-orange.com code blog logo I build web applications using ASP.NET and have a passion for javascript. Enjoy MVC and Orchard CMS, and I do TDD whenever I can. I like clean code. Love to spend time with my wife and our three children. My profile on Stack Exchange, a network of free, community-driven Q&A sites

About Anton

shades-of-orange.com code blog logo I'm a software developer at teamaton. I code in C# and work with MVC, Orchard, SpecFlow, Coypu and NHibernate. I enjoy beach volleyball, board games and Coke.