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)
// using Raven.Client.Documents;
// using Raven.Client.Documents.Linq;
// ==================================

int numberOfOrders = await asyncSession
    .Query<Order>()
     // Calling 'Where' from Raven.Client.Documents.Linq
    .Where(order => order.ShipTo.Country == "UK")
     // Calling 'CountAsync' from Raven.Client.Documents
    .CountAsync();

// The query returns the NUMBER of orders shipped to UK (Int32)
// using Raven.Client.Documents.Session;
// =====================================

int numberOfOrders = session.Advanced
    .DocumentQuery<Order>()
    .WhereEquals(order => order.ShipTo.Country, "UK")
     // Calling 'Count' from Raven.Client.Documents.Session
    .Count();

// The query returns the NUMBER of orders shipped to UK (Int32)
from "Orders"
where ShipTo.Country == "UK" limit 0, 0

// The RQL generated will trigger query execution
// however, no documents are returned (limit is set 0)

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)
// using Raven.Client.Documents;
// using Raven.Client.Documents.Linq;
// ==================================

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

// The query returns the NUMBER of orders shipped to UK (Int64)
// using Raven.Client.Documents.Session;
// =====================================

long numberOfOrders = session.Advanced
    .DocumentQuery<Order>()
    .WhereEquals(order => order.ShipTo.Country, "UK")
     // Calling 'LongCount' from  Raven.Client.Documents.Session
    .LongCount();

// The query returns the NUMBER of orders shipped to UK (Int64)
from "Orders"
where ShipTo.Country == "UK" limit 0, 0

// The RQL generated will trigger query execution
// however, no documents are returned (limit is set 0)

Get count from query stats

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

  • The number of results is available in the QueryStatistics object as:

    • TotalResults - an Int32 value
    • LongTotalResults - an Int64 value
  • Learn more in Get Query Statistics.