Count Query Results
-
The following options are available to count query results:
Count
-
When the number of resulting items is expected to be an
Int32
variable,
useCount
in a synchronous session (orCountAsync
in an async session). -
Count
is implemented inSystem.Linq
.
CountAsync
is implemented inRaven.Client.Documents
. -
An
OverflowException
will be thrown if the number of items exceedsInt32.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,
useLongCount
in a synchronous session (orLongCountAsync
in an async session). -
LongCount
is implemented in bothRaven.Client.Documents
&System.Linq
(use as needed).
LongCountAsync
is implemented inRaven.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 valueLongTotalResults
- an Int64 value
-
Learn more in Get Query Statistics.