---
title: "Session: Querying: How to Use NOT Operator"
url: "https://docs.ravendb.net/6.2/client-api/session/querying/document-query/how-to-use-not-operator"
version: "6.2"
language: "csharp"
---

# Session: Querying: How to Use NOT Operator

> Client language: C#. Also available for [Java](https://docs.ravendb.net/6.2/client-api/session/querying/document-query/how-to-use-not-operator.java.md), [Node.js](https://docs.ravendb.net/6.2/client-api/session/querying/document-query/how-to-use-not-operator.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."

`IDocumentQuery` contains `NOT` operator which can be used to negate **next** predicate

> **Note**
>
> `NOT` operator cannot be used alone without succeeding predicate.

## Example I

```csharp
// load all entities from 'Employees' collection
// where FirstName NOT equals 'Robert'
List<Employee> employees = session
  .Advanced
  .DocumentQuery<Employee>()
  .Not
  .WhereEquals(x => x.FirstName, "Robert")
  .ToList();
```

## Example II

```csharp
// load all entities from 'Employees' collection
// where FirstName NOT equals 'Robert'
// and LastName NOT equals 'King'
List<Employee> employees = session
  .Advanced
  .DocumentQuery<Employee>()
  .Not
  .OpenSubclause()
  .WhereEquals(x => x.FirstName, "Robert")
  .AndAlso()
  .WhereEquals(x => x.LastName, "King")
  .CloseSubclause()
  .ToList();
```

## Example III

```csharp
// load all entities from 'Employees' collection
// where FirstName NOT equals 'Robert'
// and LastName NOT equals 'King'
// identical to 'Example II' but 'WhereNotEquals' is used
List<Employee> employees = session
  .Advanced
  .DocumentQuery<Employee>()
  .WhereNotEquals(x => x.FirstName, "Robert")
  .AndAlso()
  .WhereNotEquals(x => x.LastName, "King")
  .ToList();
```

## See also

- [What is Document Query](https://docs.ravendb.net/6.2/client-api/session/querying/document-query/what-is-document-query.md)
- [How to Use Lucene](https://docs.ravendb.net/6.2/client-api/session/querying/document-query/how-to-use-lucene.md)
