Get Row Count InvalidCast Exception from ScalarQuery
4 answers
Which database are you using? It could be that count doesn't return int.
You can also try using http://api.castleproject.org/html/T_Castle_ActiveRecord_Queries_CountQuery.htm
0
source to share
Not really an answer to the question, but a recommendation: if you want to avoid the hassle of having to issue the query at all on your own, just use ActiveRecordMediator<T>.Count()
(which has overloads that take filter criteria / strings if you want a conditional number) and everything returns int
to all databases ...
0
source to share
Based on testing the answers provided to date, worked for me (including where clause):
// Option 1
int result = ActiveRecordMediator<Post>.Count("BlogId = ?", blogId);
// Option 2
CountQuery query = new CountQuery(typeof(Post), "BlogId = ?", blogId);
int result = ActiveRecordMediator.ExecuteQuery(query);
// Option 3
ScalarQuery<long> query= new ScalarQuery<long>(typeof(Post),
"SELECT COUNT(*) FROM Post WHERE BlogId = ?", blogId);
long result = query.Execute();
0
source to share