Count Query Results
-
The following options are available to count query results:
Count
-
When the number of resulting items is expected to be an
Int32variable,
useCountin a synchronous session (orCountAsyncin an async session). -
Countis implemented inSystem.Linq.
CountAsyncis implemented inRaven.Client.Documents. -
An
OverflowExceptionwill be thrown if the number of items exceedsInt32.MaxValue.
- Query
- Query_async
- DocumentQuery
- RQL
// 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
Int64variable,
useLongCountin a synchronous session (orLongCountAsyncin an async session). -
LongCountis implemented in bothRaven.Client.Documents&System.Linq(use as needed).
LongCountAsyncis implemented inRaven.Client.Documents.
- Query
- Query_async
- DocumentQuery
- RQL
// 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 total number of results is available in the
TotalResultsproperty of theQueryStatisticsobject.
Learn more in Get Query Statistics.