---
title: "Count Query Results"
url: "https://docs.ravendb.net/6.2/client-api/session/querying/how-to-count-query-results"
version: "6.2"
language: "csharp"
---

# Count Query Results

> Client language: C#. Also available for [Python](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-count-query-results.python.md), [PHP](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-count-query-results.php.md), [Node.js](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-count-query-results.nodejs.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

> **Note**
>
> - The following options are available to **count query results**:
>
>   - [Count](#count)
>
>   - [LongCount](#longcount)
>
>   - [Get number of results from query stats](#get-count-from-query-stats)

## 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`**.

> **Note**
>
> ### Query
>
> ```csharp
> // 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)
> ```
>
> ### Query\_async
>
> ```csharp
> // 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)
> ```
>
> ### DocumentQuery
>
> ```csharp
> // 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)
> ```
>
> ### RQL
>
> ```sql
> 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`.

> **Note**
>
> ### Query
>
> ```csharp
> // 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)
> ```
>
> ### Query\_async
>
> ```csharp
> // 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)
> ```
>
> ### DocumentQuery
>
> ```csharp
> // 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)
> ```
>
> ### RQL
>
> ```sql
> 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 `TotalResults` property of the `QueryStatistics` object.  
  Learn more in [Get Query Statistics](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-get-query-statistics.md).

## See also

- [Query overview](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-query.md)
- [What is a Document Query](https://docs.ravendb.net/6.2/client-api/session/querying/document-query/what-is-document-query.md)
- [Filter by Field Presence](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-filter-by-field.md)
- [Get Query Statistics](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-get-query-statistics.md)
- [Filtering](https://docs.ravendb.net/6.2/indexes/querying/filtering.md)
- [RQL - Raven Query Language](https://docs.ravendb.net/6.2/client-api/session/querying/what-is-rql.md)
