{"record":{"id":"b5896c6206b3dd88","repo":"dotnet/efcore","slug":"a-fromsqlexpression-has-an-invalid-arguments-expre","errorCode":null,"errorMessage":"A FromSqlExpression has an invalid arguments expression type '{expressionType}' or value type '{valueType}'.","messagePattern":"A FromSqlExpression has an invalid arguments expression type '(.+?)' or value type '(.+?)'\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Query/Internal/CosmosQuerySqlGenerator.cs","lineNumber":493,"sourceCode":"                break;\n            }\n\n            case ConstantExpression { Value: object[] constantValues }:\n            {\n                substitutions = new string[constantValues.Length];\n                for (var i = 0; i < constantValues.Length; i++)\n                {\n                    var value = constantValues[i];\n                    var typeMapping = typeMappingSource.FindMapping(value.GetType());\n                    Check.DebugAssert(typeMapping is not null, \"Could not find type mapping for FromSql parameter\");\n                    substitutions[i] = ((CosmosTypeMapping)typeMapping).GenerateSqlLiteral(value);\n                }\n\n                break;\n            }\n\n            default:\n                throw new ArgumentOutOfRangeException(\n                    nameof(fromSqlExpression),\n                    fromSqlExpression.Arguments,\n                    CosmosStrings.InvalidFromSqlArguments(\n                        fromSqlExpression.Arguments.GetType(),\n                        fromSqlExpression.Arguments is ConstantExpression constantExpression\n                            ? constantExpression.Value?.GetType()\n                            : null));\n        }\n\n        // ReSharper disable once CoVariantArrayConversion\n        // InvariantCulture not needed since substitutions are all strings\n        sql = string.Format(sql, substitutions);\n\n        _sqlBuilder.AppendLine(\"(\");\n\n        using (_sqlBuilder.Indent())\n        {\n            _sqlBuilder.AppendLines(sql);","sourceCodeStart":475,"sourceCodeEnd":511,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Query/Internal/CosmosQuerySqlGenerator.cs#L475-L511","documentation":"Thrown by the Cosmos SQL generator when processing a FromSqlExpression (FromSqlRaw/FromSqlInterpolated) whose Arguments node is neither a QueryParameterExpression backed by an object[] nor a ConstantExpression holding an object[]. The Cosmos provider only supports interpolating array-style parameter values into raw SQL; any other expression shape is considered malformed and aborts SQL generation. This is an internal-contract failure that usually surfaces a bug in how FromSql arguments were constructed upstream.","triggerScenarios":"Calling FromSqlRaw or FromSqlInterpolated on a Cosmos DbSet where the arguments object passed is not a plain object[] (e.g. an IEnumerable, a single scalar, a List<T>, or a parameter whose value is not an array). Also reachable if query preprocessing rewrites the FromSql arguments expression into an unexpected node type.","commonSituations":"Porting a relational query that used IEnumerable or a single value as FromSql arguments to Cosmos. Passing a List<object> instead of object[]. Custom query visitors that manipulate FromSqlExpression.Arguments.","solutions":["Pass arguments as object[] (or a FormattableString for FromSqlInterpolated) so the Cosmos provider can enumerate and inline them.","If using a List<T> or IEnumerable, materialize it to an array first: list.ToArray().","Avoid FromSql on Cosmos unless necessary; prefer LINQ which the provider translates natively.","If writing a custom expression visitor, do not replace FromSqlExpression.Arguments with a non-array expression."],"exampleFix":"// before\nvar ids = new List<int> { 1, 2, 3 };\nvar blogs = context.Blogs.FromSqlRaw($\"SELECT * FROM c WHERE c.id IN ({ids})\").ToList();\n// after\nvar blogs = context.Blogs.FromSqlInterpolated($\"SELECT * FROM c WHERE c.id IN ({string.Join(\",\", ids)})\").ToListAsync();","handlingStrategy":"validation","validationCode":"// Validate arguments before FromSql\nobject[] args = arguments as object[]\n    ?? (arguments as IEnumerable<object>)?.ToArray()\n    ?? throw new ArgumentException(\"FromSql arguments must be an object[]\");\nvar query = context.Blogs.FromSqlRaw(\"SELECT * FROM c\", args);","typeGuard":"static bool IsSupportedFromSqlArgs(object args)\n    => args is object[] || args is FormattableString;","tryCatchPattern":null,"preventionTips":["Always pass object[] or FormattableString to FromSql on Cosmos.","Prefer LINQ over FromSql to avoid argument-shape pitfalls.","If you must use FromSql, wrap argument construction in a helper that enforces object[]."],"tags":["cosmos","fromsql","query","translation","arguments"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}