---
title: "RVN002 - RavenDB query operator after projection"
description: "RVN002* detects query operators placed after a ProjectInto or Select projection where they can refer to the wrong fields."
url: "https://docs.ravendb.net/7.2/client-api/csharp-code-analyzers/RVN002"
version: "7.2"
---

# RVN002 - RavenDB query operator after projection

> 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**
>
> - **RVN002** detects query operators placed after a `ProjectInto` or `Select` projection where they can refer to the wrong fields.
>
> - **Default severity**: *Info*  
>   **Automated code fix**: *No*
>
> - In this article:
>   - [Description](#description)
>   - [Operators flagged by RVN002](#operators-flagged-by-rvn002)
>     - [After `ProjectInto`](#after-projectinto)
>     - [After `Select`](#after-select)
>     - [Operators not flagged](#operators-not-flagged)
>   - [Trigger condition](#trigger-condition)
>     - [When is RVN002 reported?](#when-is-rvn002-reported)
>     - [When is RVN002 not reported?](#when-is-rvn002-not-reported)
>   - [How to resolve](#how-to-resolve)
>     - [Before (triggers RVN002)](#before-triggers-rvn002)
>     - [After (correct)](#after-correct)
>   - [Limitations](#limitations)
>   - [Related C# analyzers](#related-c-analyzers)

## Description {#description}

RavenDB LINQ query operators such as `Where`, `OrderBy`, `Search`, `Spatial`, `VectorSearch`, `Filter`, `GroupBy`,  
and their variants bind against the **source document or index fields**.

A projection such as `.ProjectInto<T>()` or `.Select(x => new { … })` can change the query's element type.  
A lambda passed to an operator after the projection refers to members of the projected type, while the server still uses the source fields.
If a projected member's name does not match its source field's name, the query can silently produce incorrect results or throw at runtime.

RVN002 reports operator and projection combinations that can cause these problems.  
A reported query may still work when the referenced field names match; see [Limitations](#limitations).

## Operators flagged by RVN002 {#operators-flagged-by-rvn002}

RVN002 checks the operators listed below after `ProjectInto` or `Select`.  
After `Select`, some of these operators are not flagged; see [After `Select`](#after-select).

### After `ProjectInto`

After `ProjectInto`, the following operators are flagged:

- **Filtering:** `Where`, `Filter`.
- **Ordering:** `OrderBy`, `OrderByDescending`, `ThenBy`, `ThenByDescending`.
- **Ordering by score:** `OrderByScore`, `OrderByScoreDescending`, `ThenByScore`, `ThenByScoreDescending`.
- **Search:** `Search`, `MoreLikeThis`, `VectorSearch`.
- **Spatial:** `Spatial`, `OrderByDistance`, `OrderByDistanceDescending`.
- **Grouping:** `GroupBy`, `GroupByArrayValues`, `GroupByArrayContent`.

### After `Select`

After `Select`, the operators listed under [After `ProjectInto`](#after-projectinto) are flagged, with the exceptions below.

`Where`, `OrderBy`, `OrderByDescending`, `ThenBy`, and `ThenByDescending` are not flagged when the `Select` either:

- Returns the source element unchanged, such as `o => o`.
- Creates an anonymous object in which **every member** directly copies a top-level member of the lambda parameter under the same name,  
  such as `o => new { o.Company, o.Total }` or `o => new { Company = o.Company }`.

They are flagged after other projection forms, including renamed or computed members, nested member paths, DTO projections,
and members read from captured variables, such as `o => new { captured.Name }`.

### Operators not flagged

Operators not in the list above, such as `Include`, `Skip`, and `Take`, are not flagged after a projection.

## Trigger condition {#trigger-condition}

### When is RVN002 reported?

RVN002 checks explicit method calls in RavenDB LINQ queries (`session.Query<T>()`, sync or async).  
RVN002 is reported on the name of each flagged operator that follows `ProjectInto` or `Select`,
according to the conditions described in [Operators flagged by RVN002](#operators-flagged-by-rvn002).

For each operator, RVN002 uses the nearest preceding `ProjectInto` or `Select` in the chain to determine which conditions apply.

RVN002 follows projected queries through local-variable initializers,
so a flagged operator can appear in a later statement that uses the local variable.

### When is RVN002 not reported?

- Operators placed before a projection, or not listed under [Operators flagged by RVN002](#operators-flagged-by-rvn002).
- `Where`, `OrderBy`, `OrderByDescending`, `ThenBy`, and `ThenByDescending` after a `Select` that meets the exceptions described in [After `Select`](#after-select).
- Operators and projections written as C# query-syntax clauses, such as `select … into … where …`.
- LINQ over in-memory collections, for example after `ToList()`, or other queries that do not use RavenDB's LINQ provider.
- Operators after `SelectMany` or `GroupBy`, when the chain contains no earlier `Select` or `ProjectInto`.  
  RVN002 does not treat `SelectMany` or `GroupBy` as projections.

## How to resolve {#how-to-resolve}

Apply filtering, ordering, and other source-based operators **before** the projection, using the original document or index fields.
Place the projection after these operators and before materializing the results, for example via `ToList()` or `ToListAsync()`.

For `Where`, `OrderBy`, `OrderByDescending`, `ThenBy`, and `ThenByDescending` after `Select`, another option is to use an anonymous-object projection
in which every member directly copies a top-level member of the lambda parameter under the same name; see [After `Select`](#after-select).

---

### Before (triggers RVN002)

Here, `CompanyId` is a projection alias, not a source field.  
The `Where` must use the source field `Company` before the projection.

```csharp
var results = session.Query<Order>()
    .Select(o => new { CompanyId = o.Company })
    // Triggers RVN002: Where uses the projected alias CompanyId.
    .Where(x => x.CompanyId == "companies/1-A")
    .Take(50)
    .ToList();
```

### After (correct)

```csharp
var results = session.Query<Order>()
    .Where(o => o.Company == "companies/1-A")
    .Select(o => new { CompanyId = o.Company })
    .Take(50)
    .ToList();
```

## Limitations {#limitations}

- After `ProjectInto`, RVN002 reports every listed operator without checking whether the referenced field exists under the same name in the source.
  A reported query may therefore still work correctly.

- After `Select`, the exception for `Where` and the four ordering operators depends on the **entire projection**, rather than only the members used by the operator.
  A renamed or computed member causes these operators to be flagged even when they use another member that keeps its source name. For example:

  ```csharp
  .Select(o => new { o.Company, RenamedTotal = o.Total })
  .Where(x => x.Company == "companies/1-A")
  ```

- Tracking across statements is limited to local-variable initializers. Later assignments are not followed,  
  so diagnostics may be missed or based on an earlier query value.

- RVN002 does not trace a projected query through helper-method returns, parameters, fields, or properties.

## Related C# analyzers {#related-c-analyzers}

- [RVN003](https://docs.ravendb.net/7.2/client-api/csharp-code-analyzers/RVN003.md)  
  Reports repeated `ProjectInto` calls in the same query chain.

- [RVN007](https://docs.ravendb.net/7.2/client-api/csharp-code-analyzers/RVN007.md)  
  Reports fields used for filtering, sorting, or searching in a static-index query when the index's map function does not define them.

## See also

- [C# code analyzers overview](https://docs.ravendb.net/7.2/client-api/csharp-code-analyzers/overview.md)
- [Project query results](https://docs.ravendb.net/7.2/client-api/session/querying/how-to-project-query-results.md)
- [Project index query results](https://docs.ravendb.net/7.2/indexes/querying/projections.md)
- [Filter query results](https://docs.ravendb.net/7.2/querying/filtering-query-results/filter-query-results.md)
- [Sort query results](https://docs.ravendb.net/7.2/querying/sorting-query-results/sort-query-results.md)
