Skip to main content

RVN003 - ProjectInto called more than once in a query chain

Description​

RavenDB allows only a single projection per query, whether the projection is defined with ProjectInto<T>() or Select(...).

RVN003 detects repeated ProjectInto calls in the same LINQ query chain at compile time.
Calling ProjectInto a second time throws InvalidOperationException ("'ProjectInto' was already called…") as soon as the second call is made, because the projection is already registered on the query provider.

RVN003 does not report other combinations of projection methods, such as ProjectInto with Select.

Trigger condition​

RVN003 checks RavenDB LINQ queries from synchronous or asynchronous sessions.

When is RVN003 reported?​

RVN003 is reported on each ProjectInto call that follows an earlier ProjectInto call in the same query chain.
This includes a projected query stored in a local variable and projected again in a later statement.

The calls can target the same type or different types, and other query operators can appear between them.

When is RVN003 not reported?​

RVN003 is not reported for:

  • A query with a single projection.
  • A combination of ProjectInto and Select, or multiple Select calls.
  • Separate ProjectInto calls that each start from the same local variable initialized with an unprojected query, rather than from the result of an earlier projection.

Separate branches are not necessarily independent query instances, so the absence of RVN003 does not guarantee that reusing a LINQ query is safe. For the supported base-query reuse pattern, see How to resolve.

How to resolve​

Remove the duplicate .ProjectInto call.
If you need additional fields, use a single projection type that contains the fields you need.

To run the same base query with multiple projections, keep the unprojected base query as a DocumentQuery
(or AsyncDocumentQuery for an asynchronous session).
Call ToQueryable() separately before each projection to create independent query instances.
See Reusing the same base query for multiple projections for synchronous and asynchronous examples.


Before (triggers RVN003)​

var projected = session.Query<Order>()
.ProjectInto<OrderSummaryDto>();

var results = projected
// Triggers RVN003: the local variable already holds a projected query.
.ProjectInto<OrderDetailsDto>()
.Take(50)
.ToList();

After (correct)​

var results = session.Query<Order>()
.ProjectInto<OrderDetailsDto>()
.Take(50)
.ToList();

Limitations​

  • RVN003 follows only the value a local variable is declared with; later assignments are not taken into account. Depending on the reassignment, this can miss a repeated projection or report one that is no longer present.

  • RVN003 does not trace earlier projections through helper-method return values, parameters, fields, properties,
    or C# query-syntax clauses. Explicit repeated calls in a visible chain can still be detected.

  • Within a recognized RavenDB query chain, RVN003 identifies ProjectInto calls by name without verifying that they resolve to RavenDB's extension method. Custom methods with the same name can also cause a report.

  • RVN002
    Reports certain query operators used after a projection, including a Where placed between two ProjectInto calls.

In this article