Skip to main content

Count Query Results

Count

  • When the number of resulting items is expected to be an Int32 variable,
    use Count in a synchronous session (or CountAsync in an async session).

  • Count is implemented in System.Linq.
    CountAsync is implemented in Raven.Client.Documents.

  • An OverflowException will be thrown if the number of items exceeds Int32.MaxValue.

// using System.Linq;
// ==================

int numberOfOrders = session
.Query<Order>()
.Where(order => order.ShipTo.Country == "UK")
// Calling 'Count' from System.Linq
.Count();

// The query returns the NUMBER of orders shipped to UK (Int32)

LongCount

  • When the number of resulting items is expected to be an Int64 variable,
    use LongCount in a synchronous session (or LongCountAsync in an async session).

  • LongCount is implemented in both Raven.Client.Documents & System.Linq (use as needed).
    LongCountAsync is implemented in Raven.Client.Documents.

// using Raven.Client.Documents;
// using Raven.Client.Documents.Linq;
// ==================================

long numberOfOrders = session
.Query<Order>()
// Calling 'Where' from Raven.Client.Documents.Linq
.Where(order => order.ShipTo.Country == "UK")
// Calling 'LongCount' from Raven.Client.Documents
.LongCount();

// The query returns the NUMBER of orders shipped to UK (Int64)

Get count from query stats

  • When executing a query, you can retrieve the query statistics, which include the total number of results.

  • The total number of results is available in the TotalResults property of the QueryStatistics object.
    Learn more in Get Query Statistics.