Where am I going wrong with the count in Hql
So I only want the count of the results not the results themselves therefore I am using count in hql. So, below is the query
(int) Session.CreateQuery("select count(*) from TableName where Lhs=Rhs").UniqueResult();
But it is giving me the error Specified cast is not valid.
.
So, can any body tell me how to cast the count to int.
Any help is very much appreciated.
Try
Convert.ToInt32(Session.CreateQuery....);
Also verify if its really returning a count or null. This could be a possibility.
Thanks
do this instead:
var temp = Session.CreateQuery("select count(*) from TableName where Lhs=Rhs").UniqueResult();
//check the type of the temp
temp.GetType();
I have a feeling that it is a Long and not an int.
The problem is in your Casting because it is returning the single result (instance that matches the query) or null. What happens if you change it the int to be nullable?
精彩评论