Reputation: 1367
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.
Upvotes: 2
Views: 1858
Reputation: 9784
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.
Upvotes: 1
Reputation: 1857
Try
Convert.ToInt32(Session.CreateQuery....);
Also verify if its really returning a count or null. This could be a possibility.
Thanks
Upvotes: 3
Reputation: 9784
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?
Upvotes: 0