I’m, building a query with OrmLite, in the form of:
var q = Db.From<MyTable>()
.SelectDistinct(t => new { A = t.Field });
After that, I count the total elements, then I read the data from the query again using database pagination:
int total = (int)await _db.CountAsync(q);
var content = await _db.SelectAsync<AContenitoreFiltroDTO>(q.Limit(req.First, req.Rows));
The issue is that total
returns an higher number of elements in comparison of what the query really returns, because Count
is implemented as SELECT COUNT(*) FROM MyTable
instead of SELECT COUNT(*) FROM (SELECT DISTINCT Field AS A FROM MyTable)
. The second approach is the right one, since it preserve the original query, distinct included.
Tested with ServiceStack 5.1.0, with both Sqlite and SQL Server providers.
EDIT:
I see that there is a RowCountAsync that implements what i’m asking, but it’s not working.
On Sql Server it crashed with: SqlException: Must declare the scalar variable '@0'
, while on Sqlite SQLiteException: unknown error Insufficient parameters supplied to the command