---
title: "Filter Query Results"
url: "https://docs.ravendb.net/6.2/indexes/querying/filtering"
version: "6.2"
language: "csharp"
---

# Filter Query Results

> Client language: C#. Also available for [Java](https://docs.ravendb.net/6.2/indexes/querying/filtering.java.md), [Python](https://docs.ravendb.net/6.2/indexes/querying/filtering.python.md), [PHP](https://docs.ravendb.net/6.2/indexes/querying/filtering.php.md), [Node.js](https://docs.ravendb.net/6.2/indexes/querying/filtering.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**
>
> - One of the most basic functionalities of querying is the ability to filter out data and return records that match a given condition.
>
> - RavenDB provides several ways to run queries, including:
>   - [Query](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-query.md) from the basic `Session` API
>   - [DocumentQuery](https://docs.ravendb.net/6.2/client-api/session/querying/document-query/what-is-document-query.md) from the `Session.Advanced` API
>   - [RQL](https://docs.ravendb.net/6.2/client-api/session/querying/what-is-rql.md) - Raven Query Language
>
> - The examples in this page demonstrate how filtering is applied by each of the above querying methods.
>
> - In this page:
>   - [Where](#where)
>   - [Where - Numeric Property](#where---numeric-property)
>   - [Where - Nested Property](#where---nested-property)
>   - [Where + Any](#where-+-any)
>   - [Where + In](#where-+-in)
>   - [Where + ContainsAny](#where-+-containsany)
>   - [Where + ContainsAll](#where-+-containsall)
>   - [Where - StartsWith](#where---startswith)
>   - [Where - EndsWith](#where---endswith)
>   - [Where - Identifier Property](#where---identifier-property)
>   - [Where - Exists](#where---exists)
>   - [Remarks](#remarks)

## Where

### Query

```csharp
IList<Employee> results = session
  .Query<Employee, Employees_ByFirstAndLastName>()                // query 'Employees/ByFirstAndLastName' index
  .Where(x => x.FirstName == "Robert" && x.LastName == "King")    // filtering predicates
  .ToList();                                                      // materialize query by sending it to server for processing
```

### DocumentQuery

```csharp
IList<Employee> results = session
  .Advanced
  .DocumentQuery<Employee, Employees_ByFirstAndLastName>()    // query 'Employees/ByFirstAndLastName' index
  .WhereEquals(x => x.FirstName, "Robert")                    // filtering predicates
  .AndAlso()                                                  // by default OR is between each condition
  .WhereEquals(x => x.LastName, "King")                       // filtering predicates
  .ToList();                                                  // materialize query by sending it to server for processing
```

### Index

```csharp
public class Employees_ByFirstAndLastName : AbstractIndexCreationTask<Employee>
{
  public Employees_ByFirstAndLastName()
  {
      Map = employees => from employee in employees
                         select new
                         {
                             FirstName = employee.FirstName,
                             LastName = employee.LastName
                         };
  }
}
```

### RQL

```sql
from index 'Employees/ByFirstAndLastName'
where FirstName = 'Robert' and LastName = 'King'
```

## Where - Numeric Property

### Query

```csharp
IList<Product> results = session
  .Query<Product, Products_ByUnitsInStock>()  // query 'Products/ByUnitsInStock' index
  .Where(x => x.UnitsInStock > 50)            // filtering predicates
  .ToList();                                  // materialize query by sending it to server for processing
```

### DocumentQuery

```csharp
IList<Product> results = session
  .Advanced
  .DocumentQuery<Product, Products_ByUnitsInStock>()  // query 'Products/ByUnitsInStock' index
  .WhereGreaterThan(x => x.UnitsInStock, 50)          // filtering predicates
  .ToList();                                          // materialize query by sending it to server for processing
```

### Index

```csharp
private class Products_ByUnitsInStock : AbstractIndexCreationTask<Product>
{
  public Products_ByUnitsInStock()
  {
      Map = products => from product in products
                        select new
                        {
                            product.UnitsInStock
                        };
  }
}
```

### RQL

```sql
from index 'Products/ByUnitsInStock'
where UnitsInStock > 50
```

## Where - Nested Property

### Query

```csharp
// return all orders that were shipped to 'Albuquerque'
IList<Order> results = session
  .Query<Order>()
  .Where(x => x.ShipTo.City == "Albuquerque")
  .ToList();
```

### DocumentQuery

```csharp
// return all orders that were shipped to 'Albuquerque'
IList<Order> results = session
  .Advanced
  .DocumentQuery<Order>()
  .WhereEquals(x => x.ShipTo.City, "Albuquerque")
  .ToList();
```

### RQL

```sql
from Orders
where ShipTo.City = 'Albuquerque'
```

### Query

```csharp
IList<Order> results = session
  .Query<Order, Order_ByOrderLinesCount>()    // query 'Order/ByOrderLinesCount' index
  .Where(x => x.Lines.Count > 50)             // filtering predicates
  .ToList();                                  // materialize query by sending it to server for processing
```

### DocumentQuery

```csharp
IList<Order> results = session
  .Advanced
  .DocumentQuery<Order, Order_ByOrderLinesCount>()    // query 'Order/ByOrderLinesCount' index
  .WhereGreaterThan(x => x.Lines.Count, 50)           // filtering predicates
  .ToList();                                          // materialize query by sending it to server for processing
```

### Index

```csharp
private class Order_ByOrderLinesCount : AbstractIndexCreationTask<Order>
{
  public Order_ByOrderLinesCount()
  {
      Map = orders => from order in orders
                      select new
                      {
                          Lines_Count = order.Lines.Count
                      };
  }
}
```

### RQL

```sql
from index 'Order/ByOrderLinesCount'
where Lines.Count > 50
```

## Where + Any

`Any` is useful when you have a collection of items (e.g. `Order` contains `OrderLines`) and you want to filter out based on values from this collection. For example, let's retrieve all orders that contain an `OrderLine` with a given product.

### Query

```csharp
IList<Order> results = session
  .Query<Order, Order_ByOrderLines_ProductName>()                                 // query 'Order/ByOrderLines/ProductName' index
  .Where(x => x.Lines.Any(l => l.ProductName == "Teatime Chocolate Biscuits"))    // filtering predicates
  .ToList();                                                                      // materialize query by sending it to server for processing
```

### DocumentQuery

```csharp
IList<Order> results = session
  .Advanced
  .DocumentQuery<Order, Order_ByOrderLines_ProductName>()         // query 'Order/ByOrderLines/ProductName' index
  .WhereEquals("Lines_ProductName", "Teatime Chocolate Biscuits") // filtering predicates
  .ToList();                                                      // materialize query by sending it to server for processing
```

### Index

```csharp
public class Order_ByOrderLines_ProductName : AbstractIndexCreationTask<Order>
{
  public Order_ByOrderLines_ProductName()
  {
      Map = orders => from order in orders
                      select new
                      {
                          Lines_ProductName = order.Lines.Select(x => x.ProductName)
                      };
  }
}
```

### RQL

```sql
from index 'Order/ByOrderLinesCount'
where Lines_ProductName = 'Teatime Chocolate Biscuits'
```

## Where + In

When you want to check a single value against multiple values, the `In` operator can become handy. To retrieve all employees where `FirstName` is either `Robert` or `Nancy`, we can issue the following query:

### Query

```csharp
IList<Employee> results = session
  .Query<Employee, Employees_ByFirstAndLastName>()    // query 'Employees/ByFirstAndLastName' index
  .Where(x => x.FirstName.In("Robert", "Nancy"))      // filtering predicates (remember to add `Raven.Client.Linq` namespace to usings)
  .ToList();                                          // materialize query by sending it to server for processing
```

### DocumentQuery

```csharp
IList<Employee> results = session
  .Advanced
  .DocumentQuery<Employee, Employees_ByFirstAndLastName>()    // query 'Employees/ByFirstAndLastName' index
  .WhereIn(x => x.FirstName, new[] { "Robert", "Nancy" })     // filtering predicates
  .ToList();                                                  // materialize query by sending it to server for processing
```

### Index

```csharp
public class Employees_ByFirstAndLastName : AbstractIndexCreationTask<Employee>
{
  public Employees_ByFirstAndLastName()
  {
      Map = employees => from employee in employees
                         select new
                         {
                             FirstName = employee.FirstName,
                             LastName = employee.LastName
                         };
  }
}
```

### RQL

```sql
from index 'Employees/ByFirstAndLastName'
where FirstName IN ('Robert', 'Nancy')
```

> **Warning:** Important {#important}
>
> Remember to add the `Raven.Client.Documents.Linq` namespace to usings if you want to use `In` extension method.

## Where + ContainsAny

To check if enumeration contains **any** of the values from a specified collection, you can use the `ContainsAny` method.

Let's assume that we want to return all `BlogPosts` that contain any of the specified `Tags`.

### Query

```csharp
IList<BlogPost> results = session
  .Query<BlogPost, BlogPosts_ByTags>()                                    // query 'BlogPosts/ByTags' index
  .Where(x => x.Tags.ContainsAny(new[] { "Development", "Research" }))    // filtering predicates (remember to add `Raven.Client.Linq` namespace to usings)
  .ToList();                                                              // materialize query by sending it to server for processing
```

### DocumentQuery

```csharp
IList<BlogPost> results = session
  .Advanced
  .DocumentQuery<BlogPost, BlogPosts_ByTags>()                // query 'BlogPosts/ByTags' index
  .ContainsAny("Tags", new[] { "Development", "Research" })   // filtering predicates
  .ToList();                                                  // materialize query by sending it to server for processing
```

### Index

```csharp
public class BlogPosts_ByTags : AbstractIndexCreationTask<BlogPost>
{
  public BlogPosts_ByTags()
  {
      Map = posts => from post in posts
                     select new
                     {
                         post.Tags
                     };
  }
}
```

### RQL

```sql
from index 'BlogPosts/ByTags'
where Tags IN ('Development', 'Research')
```

> **Warning:** Important {#important-1}
>
> Remember to add the `Raven.Client.Documents.Linq` namespace to usings if you want to use the `ContainsAny` extension method.

## Where + ContainsAll

To check if an enumeration contains **all** of the values from a specified collection, you can use the `ContainsAll` method.

Let's assume that we want to return all the `BlogPosts` that contain all of the specified `Tags`.

### Query

```csharp
IList<BlogPost> results = session
  .Query<BlogPost, BlogPosts_ByTags>()                                    // query 'BlogPosts/ByTags' index
  .Where(x => x.Tags.ContainsAll(new[] { "Development", "Research" }))    // filtering predicates (remember to add `Raven.Client.Linq` namespace to usings)
  .ToList();                                                              // materialize query by sending it to server for processing
```

### DocumentQuery

```csharp
IList<BlogPost> results = session
  .Advanced
  .DocumentQuery<BlogPost, BlogPosts_ByTags>()                // query 'BlogPosts/ByTags' index
  .ContainsAll("Tags", new[] { "Development", "Research" })   // filtering predicates
  .ToList();                                                  // materialize query by sending it to server for processing
```

### Index

```csharp
public class BlogPosts_ByTags : AbstractIndexCreationTask<BlogPost>
{
  public BlogPosts_ByTags()
  {
      Map = posts => from post in posts
                     select new
                     {
                         post.Tags
                     };
  }
}
```

### RQL

```sql
from index 'BlogPosts/ByTags'
where Tags ALL IN ('Development', 'Research')
```

> **Warning:** Important {#important-2}
>
> Remember to add the `Raven.Client.Documents.Linq` namespace to usings if you want to use the `ContainsAll` extension method.

## Where - StartsWith

### Query

```csharp
// return all products which name starts with 'ch'
IList<Product> results = session
  .Query<Product>()
  .Where(x => x.Name.StartsWith("ch"))
  .ToList();
```

### DocumentQuery

```csharp
// return all products which name starts with 'ch'
IList<Product> results = session
  .Advanced
  .DocumentQuery<Product>()
  .WhereStartsWith(x => x.Name, "ch")
  .ToList();
```

### RQL

```sql
from Products 
where startsWith(Name, 'ch')
```

## Where - EndsWith

### Query

```csharp
// return all products which name ends with 'ra'
IList<Product> results = session
  .Query<Product>()
  .Where(x => x.Name.EndsWith("ra"))
  .ToList();
```

### DocumentQuery

```csharp
// return all products which name ends with 'ra'
IList<Product> results = session
  .Advanced
  .DocumentQuery<Product>()
  .WhereEndsWith(x => x.Name, "ra")
  .ToList();
```

### RQL

```sql
from Products 
where endsWith(Name, 'ra')
```

## Where - Identifier Property

Once a property used in the `Where` clause is recognized as an identity property of a given entity type
(according to [`FindIdentityProperty` convention](https://docs.ravendb.net/6.2/client-api/configuration/identifier-generation/global.md#findidentityproperty))
and there aren't any other fields involved in the query, then it is called a "collection query".
Simple collection queries that ask about documents with given IDs or where identifiers start with a given prefix
and don't require any additional handling like ordering, full-text searching, etc, are handled directly by the storage engine.
It means that querying by ID doesn't create an auto-index and has no extra cost. In terms of efficiency, it is the same as
loading documents with [`session.Load`](https://docs.ravendb.net/6.2/client-api/session/loading-entities.md) usage.

### Query

```csharp
Order order = session
  .Query<Order>()
  .Where(x => x.Id == "orders/1-A")
  .FirstOrDefault();
```

### DocumentQuery

```csharp
Order order = session
  .Advanced
  .DocumentQuery<Order>()
  .WhereEquals(x => x.Id, "orders/1-A")
  .FirstOrDefault();
```

### RQL

```sql
from Orders
where id() = 'orders/1-A'
```

### Query

```csharp
IList<Order> orders = session
  .Query<Order>()
  .Where(x => x.Id.StartsWith("orders/1"))
  .ToList();
```

### DocumentQuery

```csharp
IList<Order> orders = session
  .Advanced
  .DocumentQuery<Order>()
  .WhereStartsWith(x => x.Id, "orders/1")
  .ToList();
```

### RQL

```sql
from Orders
where startsWith(id(), 'orders/1')
```

## Where - Exists

To find all documents in a collection that have a specified field, see [How to Filter by Field Presence](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-filter-by-field.md).

To find all documents in a collection that don't have a specified field, see [How to Filter by Non-Existing Field](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-filter-by-non-existing-field.md).

## Remarks

> **Info**
>
> `Query` and `DocumentQuery` are converting predicates to the `IndexQuery` class
> so they can issue a query from a **low-level operation method**.

## See also

- [Query Overview](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-query.md)
- [Indexing Basics](https://docs.ravendb.net/6.2/indexes/indexing-basics.md)
- [Query an Index](https://docs.ravendb.net/6.2/indexes/querying/query-index.md)
- [Paging](https://docs.ravendb.net/6.2/indexes/querying/paging.md)
- [Sorting](https://docs.ravendb.net/6.2/indexes/querying/sorting.md)
