从 ScalarQuery 获取行计数 InvalidCast 异常
ScalarQuery<int> query = new ScalarQuery<int>(typeof(Role),
"select count(role.RoleId) from Role as role");
return query.Execute();
它因 invalidcast 异常而失败,但当 count 替换为 max 时成功。
ScalarQuery<int> query = new ScalarQuery<int>(typeof(Role),
"select count(role.RoleId) from Role as role");
return query.Execute();
It fails with the invalidcast exception but succeeds when count is replaced with max.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不完全是问题的答案,但建议:如果您想避免自己发出查询的麻烦,那么只需使用
ActiveRecordMediator.Count()
(它有如果您想要条件计数,则采用条件/过滤字符串的重载)并且所有数据库都返回int
。Not exactly the answer to the question, but a recommendation: if you want to avoid the hassle of having to issue a query at all yourself, then just use
ActiveRecordMediator<T>.Count()
(which has overloads that take criteria / filter strings if you want a conditional count) and all returnint
against all databases.根据测试迄今为止给出的答案,以下内容对我有用(包括 where 子句):
Based on testing the answers given to date, the following worked for me (including a where clause):
编辑:某些数据库会为计数查询返回很长的值。 例如 SQL Server。
Edit: Some databases will return long for count queries. For example SQL Server.
你使用什么数据库? 可能是 count 不返回 int。
您也可以尝试使用 http://api.castleproject.org/html/T_Castle_ActiveRecord_Queries_CountQuery.htm
What database are you using? Could be that count does not return an int.
Your could also try using http://api.castleproject.org/html/T_Castle_ActiveRecord_Queries_CountQuery.htm