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();
これは、invalidcast 例外で失敗しますが、count を max に置き換えると成功します。
編集: 一部のデータベースは、count クエリに対して long を返します。たとえば、SQL サーバー。
ScalarQuery<long> query = new ScalarQuery<long>(typeof(Role),
"select count(r) from Role r");
return query.Execute();
どのデータベースを使用していますか? count が int を返さない可能性があります。
http://api.castleproject.org/html/T_Castle_ActiveRecord_Queries_CountQuery.htmを使用することもできます
これまでに与えられた回答のテストに基づいて、次のことがうまくいきました(where句を含む):
// 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();
質問に対する正確な答えではありませんが、推奨事項:自分でクエリを発行する手間を避けたい場合は、ActiveRecordMediator<T>.Count()
(条件付きカウントが必要な場合は基準/フィルター文字列を取るオーバーロードがあります)とallint
はすべてのデータベースに対して返されます。