dotnet/efcore · error · InvalidOperationException
Argument with ref/out: {argument}
Error message
Argument with ref/out: {argument} What it means
`VisitArgument` checks `argument.RefKindKeyword` and throws `InvalidOperationException` if the argument carries a `ref`/`out`/`in` keyword. LINQ expression trees have no representation for by-reference parameters, so such arguments are unsupported.
Source
Thrown at src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs:218
return New(
new FakeConstructorInfo(anonymousType, parameterInfos),
arguments: arguments,
memberInfos);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override Expression VisitArgument(ArgumentSyntax argument)
=> VisitArgument(argument, expectedType: null);
private Expression VisitArgument(ArgumentSyntax argument, Type? expectedType)
=> !argument.RefKindKeyword.IsKind(SyntaxKind.None)
? throw new InvalidOperationException($"Argument with ref/out: {argument}")
: Visit(argument.Expression, expectedType);
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override Expression VisitArrayCreationExpression(ArrayCreationExpressionSyntax arrayCreation)
{
if (_semanticModel.GetTypeInfo(arrayCreation).Type is not IArrayTypeSymbol arrayTypeSymbol)
{
throw new InvalidOperationException($"ArrayCreation: non-array type symbol: {arrayCreation}");
}
if (arrayTypeSymbol.Rank > 1)
{
throw new NotImplementedException($"ArrayCreation: multi-dimensional array: {arrayCreation}");View on GitHub (pinned to dbf9771522)
Solutions
- Hoist the call out of the query and capture its result as a local variable instead.
- Switch to an overload that returns a value rather than using `out` (e.g. `GetValueOrDefault`).
- Restructure the query so no by-reference arguments appear inside it.
Example fix
// before var q = ctx.Items.Where(i => lookup.TryGetValue(i.Code, out var name) && name == "x"); // after var names = ctx.Items.Select(i => i.Code).Where(c => lookup.ContainsKey(c)).ToList(); var q = ctx.Items.Where(i => names.Contains(i.Code));
Defensive patterns
Strategy: validation
Validate before calling
// Reject ref/out/in arguments before translation by scanning the tree.
using Microsoft.CodeAnalysis.CSharp.Syntax;
bool hasRefOut = node.DescendantNodes().OfType<ArgumentSyntax>()
.Any(a => !a.RefKindKeyword.IsKind(SyntaxKind.None));
if (hasRefOut)
throw new NotSupportedException("Query contains ref/out/in arguments, which are unsupported in precompiled queries.");
translator.Translate(node, semanticModel); Try / catch
try { translator.Translate(node, semanticModel); }
catch (InvalidOperationException ex) when (ex.Message.StartsWith("Argument with ref/out:"))
{ /* hoist the call out of the query, then rebuild the tree */ } Prevention
- Never call `TryGetValue`/`out`-parameter APIs inside a query lambda.
- Prefer methods that return values (`GetValueOrDefault`, `ContainsKey`).
- Hoist external lookups out and capture results as locals.
When it happens
Trigger: A precompiled query that invokes a method with a `ref`/`out`/`in` argument, e.g. `dict.TryGetValue(k, out var v)`, `int.Parse(s, out _)` style calls, or `in`-parameter methods inside the query expression.
Common situations: Calling collection/dictionary APIs with `out` parameters, or passing `in` structs, inside a query that gets precompiled.
Related errors
- ArrayCreation: multi-dimensional array: {arrayCreation}
- Encountered {SyntaxKind.IsExpression} with non-constant type
- Encountered {SyntaxKind.AsExpression} with non-constant type
- BinaryExpressionSyntax with {binary.Kind()}
- ElementAccessExpressionSyntax over non-array
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/ed906ff7e4f51d95.
Report an issue: GitHub.