dotnet/efcore · error · ArgumentOutOfRangeException
A FromSqlExpression has an invalid arguments expression type
Error message
A FromSqlExpression has an invalid arguments expression type '{expressionType}' or value type '{valueType}'. What it means
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.
Source
Thrown at src/EFCore.Cosmos/Query/Internal/CosmosQuerySqlGenerator.cs:493
break;
}
case ConstantExpression { Value: object[] constantValues }:
{
substitutions = new string[constantValues.Length];
for (var i = 0; i < constantValues.Length; i++)
{
var value = constantValues[i];
var typeMapping = typeMappingSource.FindMapping(value.GetType());
Check.DebugAssert(typeMapping is not null, "Could not find type mapping for FromSql parameter");
substitutions[i] = ((CosmosTypeMapping)typeMapping).GenerateSqlLiteral(value);
}
break;
}
default:
throw new ArgumentOutOfRangeException(
nameof(fromSqlExpression),
fromSqlExpression.Arguments,
CosmosStrings.InvalidFromSqlArguments(
fromSqlExpression.Arguments.GetType(),
fromSqlExpression.Arguments is ConstantExpression constantExpression
? constantExpression.Value?.GetType()
: null));
}
// ReSharper disable once CoVariantArrayConversion
// InvariantCulture not needed since substitutions are all strings
sql = string.Format(sql, substitutions);
_sqlBuilder.AppendLine("(");
using (_sqlBuilder.Indent())
{
_sqlBuilder.AppendLines(sql);View on GitHub (pinned to dbf9771522)
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.
Example fix
// before
var ids = new List<int> { 1, 2, 3 };
var blogs = context.Blogs.FromSqlRaw($"SELECT * FROM c WHERE c.id IN ({ids})").ToList();
// after
var blogs = context.Blogs.FromSqlInterpolated($"SELECT * FROM c WHERE c.id IN ({string.Join(",", ids)})").ToListAsync(); Defensive patterns
Strategy: validation
Validate before calling
// Validate arguments before FromSql
object[] args = arguments as object[]
?? (arguments as IEnumerable<object>)?.ToArray()
?? throw new ArgumentException("FromSql arguments must be an object[]");
var query = context.Blogs.FromSqlRaw("SELECT * FROM c", args); Type guard
static bool IsSupportedFromSqlArgs(object args)
=> args is object[] || args is FormattableString; Prevention
- 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[].
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- The LINQ expression '{expression}' could not be translated.
- The LINQ expression '{expression}' could not be translated.
- The LINQ expression '{expression}' could not be translated.
- The LINQ expression '{expression}' could not be translated.
- The '{methodName}' method is not supported because the query
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/b5896c6206b3dd88.
Report an issue: GitHub.