{"record":{"id":"ed906ff7e4f51d95","repo":"dotnet/efcore","slug":"argument-with-ref-out-argument","errorCode":null,"errorMessage":"Argument with ref/out: {argument}","messagePattern":"Argument with ref/out: (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs","lineNumber":218,"sourceCode":"\n        return New(\n            new FakeConstructorInfo(anonymousType, parameterInfos),\n            arguments: arguments,\n            memberInfos);\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public override Expression VisitArgument(ArgumentSyntax argument)\n        => VisitArgument(argument, expectedType: null);\n\n    private Expression VisitArgument(ArgumentSyntax argument, Type? expectedType)\n        => !argument.RefKindKeyword.IsKind(SyntaxKind.None)\n            ? throw new InvalidOperationException($\"Argument with ref/out: {argument}\")\n            : Visit(argument.Expression, expectedType);\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public override Expression VisitArrayCreationExpression(ArrayCreationExpressionSyntax arrayCreation)\n    {\n        if (_semanticModel.GetTypeInfo(arrayCreation).Type is not IArrayTypeSymbol arrayTypeSymbol)\n        {\n            throw new InvalidOperationException($\"ArrayCreation: non-array type symbol: {arrayCreation}\");\n        }\n\n        if (arrayTypeSymbol.Rank > 1)\n        {\n            throw new NotImplementedException($\"ArrayCreation: multi-dimensional array: {arrayCreation}\");","sourceCodeStart":200,"sourceCodeEnd":236,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs#L200-L236","documentation":"`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.","triggerScenarios":"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.","commonSituations":"Calling collection/dictionary APIs with `out` parameters, or passing `in` structs, inside a query that gets precompiled.","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."],"exampleFix":"// before\nvar q = ctx.Items.Where(i => lookup.TryGetValue(i.Code, out var name) && name == \"x\");\n// after\nvar names = ctx.Items.Select(i => i.Code).Where(c => lookup.ContainsKey(c)).ToList();\nvar q = ctx.Items.Where(i => names.Contains(i.Code));","handlingStrategy":"validation","validationCode":"// Reject ref/out/in arguments before translation by scanning the tree.\nusing Microsoft.CodeAnalysis.CSharp.Syntax;\nbool hasRefOut = node.DescendantNodes().OfType<ArgumentSyntax>()\n    .Any(a => !a.RefKindKeyword.IsKind(SyntaxKind.None));\nif (hasRefOut)\n    throw new NotSupportedException(\"Query contains ref/out/in arguments, which are unsupported in precompiled queries.\");\n\ntranslator.Translate(node, semanticModel);","typeGuard":null,"tryCatchPattern":"try { translator.Translate(node, semanticModel); }\ncatch (InvalidOperationException ex) when (ex.Message.StartsWith(\"Argument with ref/out:\"))\n{ /* hoist the call out of the query, then rebuild the tree */ }","preventionTips":["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."],"tags":["ef-core","precompiled-queries","expression-trees","unsupported-construct"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}