OrmLite Count() implementations does not consider distincts

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

Please provide a complete query I can use to repro the issue.

I’m using this test to check for the error.

[Test]
public void OrmLite_RowCount_Crashes_With_Filters()
{
    var q = Db.From<LRARichiesta>()
              .Where(x => x.Id > 0);

    Assert.ThrowsAsync(typeof(System.Data.SQLite.SQLiteException), async () => await Db.RowCountAsync(q));
}

The Insufficient parameters issue with db.RowCountAsync() should now be resolved with this commit.

This change is available from v5.1.1 that’s now available on MyGet.