RVN010 - Unsupported method call inside RavenDB query expression
-
RVN010 reports calls to methods declared in your own code inside the lambdas and clauses of a RavenDB LINQ query.
-
Default severity: Info
Automated code fix: No -
In this article:
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 theFirstNameandLastNameproperties of employee documente. For calls like this, the outcome depends on where the call appears:-
In a filter or sort, such as
WhereorOrderBy, a call without a supported translation causesNotSupportedExceptionduring 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 yourToString()override becomes JavaScript'stoString(); your C# override is not executed.
-
-
Calls that do not need document data:
A method call such asDefaults.Status()can return a default value, such as"Active", without using data from a document being queried. Ino.Status == Defaults.Status(), the condition uses the document'sStatus, but theDefaults.Status()call does not depend ono. In thisWherecondition, the client evaluatesDefaults.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
SelectorSelectManylambda, or aletorselectclause) 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 RVN010 checks lambda arguments passed to: C# query clauses RVN010 checks expressions in Additional coverageQuery methods and clauses checked by RVN010
Where, OrderBy, OrderByDescending, ThenBy, ThenByDescendingSelect, SelectMany, GroupBySearch, Filter, Spatial, VectorSearchOrderByDistance, OrderByDistanceDescending, MoreLikeThisGroupByArrayValues, GroupByArrayContentwhere, 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.
ToString(), Equals(object), and GetHashCode() are also checked.OfType<T>().Any(…) predicate.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 inSelect(o => new { o.Status, Label = formatter.Label() })whenformatteris a local variable, a parameter, or an instance field or property. A call such asformatter.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 asFormatter.Shared.Label(). - Calls inside the lambda passed to a time-series
GroupBy, such asRavenQuery.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
EqualsandToStringmethods 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 theMapof 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 assession.Query<Order>().FirstOrDefault(o => …), or toFirst,Single,Any, orCount, 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 asIQueryable<T>, RVN010 follows method chains and local-variable initializers to identify a RavenDB query. It does not followIQueryable<T>parameters, fields, or properties.