dotnet/efcore · error · ArgumentException
The number of indices provided ({indicesCount}) must match t
Error message
The number of indices provided ({indicesCount}) must match the number of array segments in the JSON path ({arraySegmentCount}). What it means
Thrown by the StructuredJsonPath constructor (StructuredJsonPath.cs:32-42) as an ArgumentException when the number of provided index values does not match the number of array segments in the path. For every path segment where IsArray is true, exactly one int? index entry must be supplied (null means 'all elements'). The constructor counts array segments and compares to indices.Count.
Source
Thrown at src/EFCore.Relational/Infrastructure/StructuredJsonPath.cs:39
/// <summary>
/// Creates a new <see cref="StructuredJsonPath" /> instance.
/// </summary>
/// <param name="segments">The path segments.</param>
/// <param name="indices">
/// The index values for array index placeholders. Must have one entry for each segment
/// where <see cref="StructuredJsonPathSegment.IsArray" /> is <see langword="true" />. A
/// <see langword="null" /> entry means the indexer is unspecified (all elements) and is
/// rendered as <c>[*]</c> by default, or as <c>[]</c>.
/// </param>
public StructuredJsonPath(IReadOnlyList<StructuredJsonPathSegment> segments, IReadOnlyList<int?>? indices)
{
var arraySegmentCount = segments.Count(s => s.IsArray);
if (indices is null
? arraySegmentCount != 0
: indices.Count != arraySegmentCount)
{
throw new ArgumentException(
CoreStrings.InvalidStructuredJsonPathIndexCount(indices?.Count ?? 0, arraySegmentCount),
nameof(indices));
}
Segments = segments;
Indices = indices;
}
/// <summary>
/// Gets the path segments.
/// </summary>
public virtual IReadOnlyList<StructuredJsonPathSegment> Segments { get; }
/// <summary>
/// Gets the index values for array index placeholders. The indices are applied in order
/// to the segments where <see cref="StructuredJsonPathSegment.IsArray" /> is <see langword="true" />.
/// A <see langword="null" /> entry means the indexer is unspecified (all elements).
/// </summary>View on GitHub (pinned to dbf9771522)
Solutions
- Count the array segments (those with IsArray == true) in your segments list and provide exactly that many index entries.
- If the path has no array segments, pass null (or an empty list) for indices.
- Use a helper: var indices = segments.Where(s => s.IsArray).Select(s => (int?)0).ToList();
Example fix
// before: 2 array segments but only 1 index
var path = new StructuredJsonPath(
new[] { segProperty, segArray1, segArray2 },
new int?[] { 0 }); // throws
// after: one index per array segment
var path = new StructuredJsonPath(
new[] { segProperty, segArray1, segArray2 },
new int?[] { 0, null }); // 0 for first array, null (all) for second Defensive patterns
Strategy: validation
Validate before calling
int arraySegCount = segments.Count(s => s.IsArray);
if (indices == null && arraySegCount != 0)
throw new ArgumentException($"Path has {arraySegCount} array segments but indices is null.");
if (indices != null && indices.Count != arraySegCount)
throw new ArgumentException($"Expected {arraySegCount} indices, got {indices.Count}.");
var path = new StructuredJsonPath(segments, indices); Prevention
- Always compute the indices list from the segments: segments.Where(s => s.IsArray).Select(_ => (int?)0).ToArray().
- If your path has no array segments, pass null for indices.
- Unit-test StructuredJsonPath construction with varying array-segment counts.
When it happens
Trigger: Constructing a new StructuredJsonPath(segments, indices) where segments contains N array-type segments but indices has a different count. Passing null indices when segments contain array segments, or passing too few/too many indices.
Common situations: Building a JSON path programmatically and mismatching the array-index list length. Passing indices for non-array segments or omitting indices for array segments. Low-level API misuse when constructing structured JSON paths for queries or updates.
Related errors
- Invalid number of index sort order values: {numValues} value
- The number argument cannot be a negative number.
- The value '{value}' provided for argument '{argumentName}' m
- Exactly one of '{param1}' or '{param2}' must be set.
- The specified 'CommandTimeout' value '{value}' is not valid.
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/670c7e81bc2656d3.
Report an issue: GitHub.