Skip to main content

RVN010 - Unsupported method call inside RavenDB query expression

Description​

The RavenDB client translates a LINQ query, including the lambdas passed to Where, OrderBy, Select, and similar methods, into RQL, and sends the RQL to the server.

The RavenDB client can translate member access, operators, and a set of supported .NET and RavenDB methods,
but does not automatically translate the code inside methods you define.

  • Calls that need document data:
    For example, EmployeeHelpers.FullName(e) calculates a full name from the FirstName and LastName properties of employee document e. For calls like this, the outcome depends on where the call appears:

    • In a filter or sort, such as Where or OrderBy, a call without a supported translation causes NotSupportedException during client-side query translation.

    • In a projection (Select), expressions can be translated to JavaScript that runs on the server. Such a call can fail during translation or on the server, or behave differently from your C# implementation. For example, a call to your ToString() override becomes JavaScript's toString(); your C# override is not executed.

  • Calls that do not need document data:
    A method call such as Defaults.Status() can return a default value, such as "Active", without using data from a document being queried. In o.Status == Defaults.Status(), the condition uses the document's Status, but the Defaults.Status() call does not depend on o. In this Where condition, the client evaluates Defaults.Status() before sending the query, and RVN010 does not report the call.

    In a projection, a static call such as Defaults.Status() is reported even though it does not use document data. The call may be translated to JavaScript instead of being evaluated locally, or fail during translation.

Trigger condition​

When is RVN010 reported?​

RVN010 checks calls to methods declared in the source code of the project that contains the query. Inside a checked lambda or query clause of a RavenDB LINQ query, RVN010 reports such a call when either condition is met:

  • The call references a lambda parameter or range variable declared within the checked lambda or C# query, either in an argument or in the expression the method is called on.
  • The call appears in a projection (a Select or SelectMany lambda, or a let or select clause) and is not an instance call on an object captured from outside the query. Captured objects include local variables, parameters, and instance fields or properties.

For example, using EmployeeHelpers.FullName from the Before example section below:

var query = session.Query<Employee>()
.Where(e => EmployeeHelpers.FullName(e) == "Nancy Davolio");

RVN010 is reported on FullName, which is declared in your code and receives e.

Both method syntax and C# query syntax are checked.

Query methods and clauses checked by RVN010

Query methods

RVN010 checks lambda arguments passed to:

  • Where, OrderBy, OrderByDescending, ThenBy, ThenByDescending
  • Select, SelectMany, GroupBy
  • Search, Filter, Spatial, VectorSearch
  • OrderByDistance, OrderByDistanceDescending, MoreLikeThis
  • GroupByArrayValues, GroupByArrayContent

C# query clauses

RVN010 checks expressions in where, orderby, let, and select, both expressions in group … by, and the key expressions on both sides of equals in a join. Clauses after an into continuation are also checked.


Additional coverage

  • Static, instance, and extension methods, as well as local functions. User-defined overrides of ToString(), Equals(object), and GetHashCode() are also checked.
  • Queries in sync and async sessions, including through local variables and after operators such as OfType<T>().
  • Calls inside nested lambdas within checked expressions, such as a helper call inside an Any(…) predicate.
  • Calls inside nested C# queries within checked expressions, such as from l in o.Lines where Helper(l) select l inside a projection.

When is RVN010 not reported?​

  • Outside projections, calls that do not use a lambda parameter or range variable declared in the query, such as Defaults.Status() in .Where(o => o.Status == Defaults.Status()). This also applies to calls that use only a variable from outside the query, even a parameter of an enclosing lambda.
  • Inside projections, instance calls on objects captured from outside the query, provided the call does not reference a query lambda parameter or range variable. For example, formatter.Label() is not reported in Select(o => new { o.Status, Label = formatter.Label() }) when formatter is a local variable, a parameter, or an instance field or property. A call such as formatter.Label(o.Status) is still reported because it uses document data. The exception also does not apply to calls through static fields or properties, such as Formatter.Shared.Label().
  • Calls inside the lambda passed to a time-series GroupBy, such as RavenQuery.TimeSeries(x, "Readings").GroupBy(g => …), which the client runs locally.
  • Arguments that are not lambdas, such as the coordinates passed to OrderByDistance.
  • Members generated by the compiler, such as the Equals and ToString methods of a record.
  • Delegate invocations, such as calling a Func<…>.
  • LINQ over results already loaded into memory, for example after ToList().
  • Document queries (session.Advanced.DocumentQuery<T>()).

How to resolve​

Do one of the following:

  • Compute the value before the query if the call does not depend on document data.
    Store the result in a local variable and use that variable in the query. For example:

    var status = Defaults.Status();
    var query = session.Query<Order>()
    .Select(o => new { o.Id, Status = status });
  • Compute the value at indexing time:
    move the logic into the Map of a static index, and query the resulting index-field.
    The index map runs on the server; see RVN009 for the methods it can call.

  • Rewrite the condition or projection using document fields and operators the client can translate.

  • Apply the method on the client after loading the results.
    Filter and limit the query as much as possible first: every result the query returns is loaded into memory
    (see RVN013).


Before (triggers RVN010)​

public static class EmployeeHelpers
{
public static string FullName(Employee e) => e.FirstName + " " + e.LastName;
}

var employees = session.Query<Employee>()
// Triggers RVN010: FullName is declared in your code.
.Where(e => EmployeeHelpers.FullName(e) == "Nancy Davolio")
.Take(50)
.ToList();

After (correct)​

public class Employees_ByFullName : AbstractIndexCreationTask<Employee, Employees_ByFullName.IndexEntry>
{
public class IndexEntry
{
public string FullName { get; set; }
}

public Employees_ByFullName()
{
// The full name is computed on the server during indexing.
Map = employees => from e in employees
select new IndexEntry
{
FullName = e.FirstName + " " + e.LastName
};
}
}

var employees = session.Query<Employees_ByFullName.IndexEntry, Employees_ByFullName>()
.Where(x => x.FullName == "Nancy Davolio")
.OfType<Employee>()
.Take(50)
.ToList();

Limitations​

  • Only the methods and clauses listed above are checked. A predicate passed directly to another query operator,
    such as session.Query<Order>().FirstOrDefault(o => …), or to First, Single, Any, or Count, is not checked.

  • Methods from compiled assemblies, such as a NuGet package or a referenced DLL, are not reported.
    The absence of RVN010 does not guarantee that such calls can be translated.

  • Custom query translations registered through client conventions are not considered by RVN010.
    A method with a registered translation can still be reported.

  • Queries typed as IRavenQueryable<T> are recognized directly.
    For queries typed as IQueryable<T>, RVN010 follows method chains and local-variable initializers to identify a RavenDB query. It does not follow IQueryable<T> parameters, fields, or properties.

  • RVN009
    The index equivalent: fires when a method declared in your code is called inside an index Map or Reduce lambda.

  • RVN013
    Fires when a query result is not bounded by Take().

In this article