开发者

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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜