Search code examples
servicestackormlite-servicestack

How to select count of a table using ServiceStack OrmLite


How can I select the count from a table and include a where clause to return a long? Ideally I would use db.Count instead of db.Select. I'm just not sure how to use db.Count and cannot find documentation on it.

long totalCount = 0;
using (IDbConnection db = dbFactory.OpenDbConnection())
{
    totalCount = db.Count<Content>( ?? );
}
Console.WriteLine(totalCount);

Solution

  • You answered for you question in your comment ;) You should use Count extension method with expression parameter. Example below:

    long amout  = db.Count<Post>(x => x.Subject == "test");
    

    OrmLite generates following sql:

    SELECT Count(*) FROM POST WHERE (SUBJECT = 'test')