Search code examples
hqlcastle-activerecord

Get Row Count InvalidCast Exception from ScalarQuery


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.


Solution

  • Edit: Some databases will return long for count queries. For example SQL Server.

    ScalarQuery<long> query = new ScalarQuery<long>(typeof(Role), 
                              "select count(r) from Role r");
    return query.Execute();