---
title: "What is a DocumentQuery?"
description: "Use DocumentQuery in RavenDB for a lower-level, chainable query API that provides more control than the standard LINQ-based Query."
url: "https://docs.ravendb.net/7.2/querying/document-query/what-is-document-query"
version: "7.2"
language: "csharp"
---

# What is a DocumentQuery?

> Client language: C#. Also available for [Java](https://docs.ravendb.net/7.2/querying/document-query/what-is-document-query.java.md), [Python](https://docs.ravendb.net/7.2/querying/document-query/what-is-document-query.python.md), [PHP](https://docs.ravendb.net/7.2/querying/document-query/what-is-document-query.php.md), [Node.js](https://docs.ravendb.net/7.2/querying/document-query/what-is-document-query.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**
>
> - RavenDB Queries can be executed via `Query`, `DocumentQuery` or directly using `RQL`.  
>   Learn more in [Query Overview](https://docs.ravendb.net/7.2/querying/overview.md).
>
> - Unlike the `Query` method, the `DocumentQuery` method does Not support LINQ.  
>   However, it gives you more flexibility and control over the process of building a query,  
>   as it provides low-level querying capabilities. See [Query -vs- DocumentQuery](https://docs.ravendb.net/7.2/querying/document-query/query-vs-document-query.md) for all differences.
>
> - In this article:
>   - [`DocumentQuery` examples](#documentquery-examples)
>   - [Convert between `DocumentQuery` and `Query`](#convert-between-documentquery-and-query)
>   - [Custom methods and extensions](#custom-methods-and-extensions)
>   - [Syntax](#syntax)

## DocumentQuery examples {#documentquery-examples}

#### Query collection - no filtering

##### DocumentQuery

```csharp
// Query for all documents from 'Employees' collection
List<Employee> allEmployees = session.Advanced
    .DocumentQuery<Employee>()
    .ToList();
```

##### DocumentQuery\_async

```csharp
// Query for all documents from 'Employees' collection
List<Employee> allEmployees = await asyncSession.Advanced
    .AsyncDocumentQuery<Employee>() 
    .ToListAsync();
```

##### RQL

```sql
from "Employees"
```

#### Query collection - by ID

##### DocumentQuery

```csharp
// Query collection by document ID
Employee employee = session.Advanced
    .DocumentQuery<Employee>()
    .WhereEquals(x => x.Id, "employees/1-A")
    .FirstOrDefault();
```

##### DocumentQuery\_async

```csharp
// Query collection by document ID
Employee employee = await asyncSession.Advanced
    .AsyncDocumentQuery<Employee>()
    .WhereEquals(x => x.Id, "employees/1-A")
    .FirstOrDefaultAsync();
```

##### RQL

```sql
from "Employees" where id() == "employees/1-A"
```

#### Query collection - with filtering

##### DocumentQuery

```csharp
// Query collection - filter by document field
List<Employee> employees = session.Advanced
    .DocumentQuery<Employee>()
    .WhereEquals(x => x.FirstName, "Robert")
    .ToList();
```

##### DocumentQuery\_async

```csharp
// Query collection - filter by document field
List<Employee> employees = await asyncSession.Advanced
    .AsyncDocumentQuery<Employee>()
    .WhereEquals(x => x.FirstName, "Robert")
    .ToListAsync();
```

##### RQL

```sql
from "Employees" where FirstName == "Robert"
```

#### Query collection - with paging

##### DocumentQuery

```csharp
// Query collection - page results
List<Product> products = session.Advanced
    .DocumentQuery<Product>()
    .Skip(5)   // Skip first 5 results
    .Take(10)  // Load up to 10 entities from 'Products' collection
    .ToList();
```

##### DocumentQuery\_async

```csharp
// Query collection - page results
List<Product> products = await asyncSession.Advanced
    .AsyncDocumentQuery<Product>()
    .Skip(5)   // Skip first 5 results
    .Take(10)  // Load up to 10 entities from 'Products' collection
    .ToListAsync();
```

##### RQL

```sql
from "Products" limit 5, 10 // skip 5, take 10
```

#### Query an index

Please refer to [Querying an index](https://docs.ravendb.net/7.2/indexes/querying/query-index.md#sessionadvanceddocumentquery) for examples of querying an index using a DocumentQuery.

## Convert between DocumentQuery and Query {#convert-between-documentquery-and-query}

### DocumentQuery to Query

A `DocumentQuery` can be converted to a `Query`.  
This enables you to take advantage of all available LINQ extensions provided by RavenDB.

#### DocumentQuery

```csharp
// Define a DocumentQuery
var docQuery = session.Advanced
    .DocumentQuery<Order>(); // 'IDocumentQuery' instance

// Convert to Query
var query = docQuery.ToQueryable(); // 'IRavenQueryable' instance

// Apply any 'IRavenQueryable' LINQ extension
var queryResults = query
    .Where(x => x.Freight > 25)
    .ToList();
```

#### DocumentQuery\_async

```csharp
// Define a DocumentQuery
var docQuery = asyncSession.Advanced
    .AsyncDocumentQuery<Order>(); // 'IAsyncDocumentQuery' instance

// Convert to Query
var query = docQuery.ToQueryable(); // 'IRavenQueryable' instance

// Apply any 'IRavenQueryable' LINQ extension
var queryResults = await query
    .Where(x => x.Freight > 25) 
    .ToListAsync();
```

#### RQL

```sql
from "Orders"
where Freight > 25
```

Convert `DocumentQuery` to `Query` when you need to project data from a related document in a dynamic query.

#### DocumentQuery

```csharp
// Define a DocumentQuery
var docQuery = session.Advanced
    .DocumentQuery<Order>()
    .WhereGreaterThan("Freight", 25);

// Convert to Query
var query = docQuery.ToQueryable();

// Define the projection on the query using LINQ
var projectedQuery = from order in query
    // Load the related document
    let company = session.Load<Company>(order.Company)
    // Define the projection
    select new
    {
        Freight = order.Freight,   // data from the Order document
        CompanyName = company.Name // data from the related Company document
    };

// Execute the query
var queryResults = projectedQuery.ToList();
```

#### RQL

```sql
from "Orders" as o
where o.Freight > 25
load o.Company as c
select {
    Freight: o.Freight,
    CompanyName: c.Name
}
```

---

### Query to DocumentQuery

A `Query` can be converted to a `DocumentQuery`.  
This enables you to take advantage of the API available only for *DocumentQuery*.

#### DocumentQuery

```csharp
// Define a Query
var query = session
    .Query<Order>()
    .Where(x => x.Freight > 25);
    
// Convert to DocumentQuery
var docQuery = query.ToDocumentQuery();

// Apply a DocumentQuery method (e.g. IncludeExplanations is Not available on Query)
docQuery.IncludeExplanations(out Explanations exp);

// Execute the query
var docQueryResults = docQuery.ToList();
```

#### DocumentQuery\_async

```csharp
// Define a Query
var query = asyncSession
    .Query<Order>()
    .Where(x => x.Freight > 25);
    
// Convert to DocumentQuery
var docQuery = query.ToAsyncDocumentQuery();

// Apply a DocumentQuery method (e.g. IncludeExplanations is Not available on Query)
docQuery.IncludeExplanations(out Explanations exp);

// Execute the query
var docQueryResults = docQuery.ToListAsync();
```

#### RQL

```sql
from "Orders"
where Freight > 25
include explanations()
```

## Custom methods and extensions {#custom-methods-and-extensions}

> **Note**
>
> Several methods share the same functionality as their `Query` counterparts.  
> Refer to the corresponding documentation articles, marked with links starting with "\[Query]" in the list below.

Available custom methods and extensions:

- AddOrder
- \[Query] [AfterQueryExecuted](https://docs.ravendb.net/7.2/querying/customize-query.md#afterqueryexecuted)
- \[Query] [AfterStreamExecuted](https://docs.ravendb.net/7.2/querying/customize-query.md#afterstreamexecuted)
- \[Query] [AggregateBy](https://docs.ravendb.net/7.2/client-api/session/querying/how-to-perform-a-faceted-search.md)
- \[Query] [AggregateUsing](https://docs.ravendb.net/7.2/client-api/session/querying/how-to-perform-a-faceted-search.md)
- AndAlso
- \[Query] [BeforeQueryExecuted](https://docs.ravendb.net/7.2/querying/customize-query.md#beforequeryexecuted)
- [Boost](https://docs.ravendb.net/7.2/client-api/session/querying/text-search/boost-search-results.md)
- CloseSubclause
- CmpXchg
- ContainsAll
- ContainsAny
- [Count](https://docs.ravendb.net/7.2/client-api/session/querying/how-to-count-query-results.md)
- [CountLazily](https://docs.ravendb.net/7.2/client-api/session/querying/how-to-perform-queries-lazily.md#lazy-count-query)
- Distinct
- ExplainScores
- First
- FirstOrDefault
- Fuzzy
- GetIndexQuery
- GetQueryResult
- [GroupBy](https://docs.ravendb.net/7.2/client-api/session/querying/how-to-perform-group-by-query.md)
- [GroupByArrayValues](https://docs.ravendb.net/7.2/client-api/session/querying/how-to-perform-group-by-query.md#by-array-values)
- [GroupByArrayContent](https://docs.ravendb.net/7.2/client-api/session/querying/how-to-perform-group-by-query.md#by-array-content)
- \[Query] [Highlight](https://docs.ravendb.net/7.2/client-api/session/querying/text-search/highlight-query-results.md)
- Include
- IncludeExplanations
- Intersect
- InvokeAfterQueryExecuted
- InvokeAfterStreamExecuted
- \[Query] [Lazily](https://docs.ravendb.net/7.2/client-api/session/querying/how-to-perform-queries-lazily.md)
- [LongCount](https://docs.ravendb.net/7.2/client-api/session/querying/how-to-count-query-results.md)
- MoreLikeThis
- NegateNext
- [Not](https://docs.ravendb.net/7.2/querying/filtering-query-results/use-logical-operators.md#negate-a-condition-with-not)
- \[Query] [NoCaching](https://docs.ravendb.net/7.2/querying/customize-query.md#nocaching)
- \[Query] [NoTracking](https://docs.ravendb.net/7.2/querying/customize-query.md#notracking)
- OfType
- OpenSubclause
- [OrderBy](https://docs.ravendb.net/7.2/querying/sorting-query-results/sort-query-results.md)
- [OrderByDescending](https://docs.ravendb.net/7.2/querying/sorting-query-results/sort-query-results.md)
- \[Query] [OrderByDistance](https://docs.ravendb.net/7.2/client-api/session/querying/how-to-make-a-spatial-query.md#orderbydistance)
- \[Query] [OrderByDistanceDescending](https://docs.ravendb.net/7.2/client-api/session/querying/how-to-make-a-spatial-query.md#orderbydistancedesc)
- [OrderByScore](https://docs.ravendb.net/7.2/querying/sorting-query-results/sort-query-results.md#order-by-score)
- [OrderByScoreDescending](https://docs.ravendb.net/7.2/querying/sorting-query-results/sort-query-results.md#order-by-score)
- OrElse
- \[Query] [Projection](https://docs.ravendb.net/7.2/querying/customize-query.md#projection)
- Proximity
- \[Query] [RandomOrdering](https://docs.ravendb.net/7.2/querying/customize-query.md#randomordering)
- \[Query] [RelatesToShape](https://docs.ravendb.net/7.2/client-api/session/querying/how-to-make-a-spatial-query.md#search-by-shape)
- [Search](https://docs.ravendb.net/7.2/client-api/session/querying/text-search/full-text-search.md)
- SelectFields
- SelectTimeSeries
- Single
- SingleOrDefault
- Skip
- \[Query] [Spatial](https://docs.ravendb.net/7.2/client-api/session/querying/how-to-make-a-spatial-query.md)
- Statistics
- [SuggestUsing](https://docs.ravendb.net/7.2/client-api/session/querying/how-to-work-with-suggestions.md)
- Take
- \[Query] [Timings](https://docs.ravendb.net/7.2/querying/customize-query.md#timings)
- UsingDefaultOperator
- \[Query] [WaitForNonStaleResults](https://docs.ravendb.net/7.2/querying/customize-query.md#waitfornonstaleresults)
- Where
- WhereBetween
- [WhereEndsWith](https://docs.ravendb.net/7.2/client-api/session/querying/text-search/ends-with-query.md)
- WhereEquals
- [WhereExists](https://docs.ravendb.net/7.2/querying/filtering-query-results/filter-by-field-presence.md)
- WhereGreaterThan
- WhereGreaterThanOrEqual
- WhereIn
- WhereLessThan
- WhereLessThanOrEqual
- [WhereLucene](https://docs.ravendb.net/7.2/querying/filtering-query-results/filter-with-lucene-syntax.md)
- WhereNotEquals
- [WhereRegex](https://docs.ravendb.net/7.2/client-api/session/querying/text-search/using-regex.md)
- [WhereStartsWith](https://docs.ravendb.net/7.2/client-api/session/querying/text-search/starts-with-query.md)
- WithinRadiusOf

## Syntax {#syntax}

The available `DocumentQuery` overloads are listed in this [Syntax section](https://docs.ravendb.net/7.2/querying/overview.md#syntax) in the [Query Overview](https://docs.ravendb.net/7.2/querying/overview.md).

## See also

- [Query overview](https://docs.ravendb.net/7.2/querying/overview.md)
- [Filter with Lucene Syntax](https://docs.ravendb.net/7.2/querying/filtering-query-results/filter-with-lucene-syntax.md)
- [Querying: Basics](https://docs.ravendb.net/7.2/indexes/querying/query-index.md)
- [Query vs DocumentQuery](https://docs.ravendb.net/7.2/querying/document-query/query-vs-document-query.md)
- [Projections](https://docs.ravendb.net/7.2/indexes/querying/projections.md)
