dotnet/efcore · error · NotImplementedException

IndexExpression with multiple arguments

Error message

IndexExpression with multiple arguments

What it means

VisitIndex only renders single-argument indexers (obj[arg]) via ElementAccessExpression with one argument. An IndexExpression with more than one argument — a multidimensional array access like arr[i,j] or a multi-parameter indexer this[int,int] — has no handled rendering, so it throws NotImplementedException.

Source

Thrown at src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs:1911

                break;
            case MethodBase method:
                _methodUnsafeAccessors[method] = unsafeAccessorDeclaration;
                break;
            default:
                throw new UnreachableException();
        }

        return unsafeAccessorDeclaration;
    }

    /// <inheritdoc />
    protected override Expression VisitIndex(IndexExpression index)
    {
        using var _ = ChangeContext(ExpressionContext.Expression);

        if (index.Arguments.Count > 1)
        {
            throw new NotImplementedException("IndexExpression with multiple arguments");
        }

        Result =
            ElementAccessExpression(Translate<ExpressionSyntax>(index.Object!))
                .WithArgumentList(
                    BracketedArgumentList(
                        SingletonSeparatedList(
                            Argument(
                                Translate<ExpressionSyntax>(index.Arguments.Single())))));

        return index;
    }

    /// <inheritdoc />
    protected override Expression VisitMethodCall(MethodCallExpression call)
    {
        if (call.Method.DeclaringType is null)
        {

View on GitHub (pinned to dbf9771522)

Solutions

  1. Switch from a rectangular array to a jagged array (int[][]) so indexing becomes nested single-argument accesses arr[i][j].
  2. Replace indexer usage with an explicit method call that the translator can render.
  3. Avoid multidimensional arrays and multi-parameter indexers in precompiled query expressions.

Example fix

// before
int[,] grid;  ... grid[i, j]  // IndexExpression with 2 args -> throws
// after
int[][] grid;  ... grid[i][j]  // nested single-arg index expressions
Defensive patterns

Strategy: validation

Validate before calling

// Flag multi-argument index expressions
protected override Expression VisitIndex(IndexExpression i) { if (i.Arguments.Count > 1) Found = true; return i; }

Prevention

When it happens

Trigger: Indexing into a two-or-more-dimensional array, or invoking a multi-parameter indexer, inside a precompiled query or model lambda.

Common situations: Using rectangular (multidimensional) arrays in queries; types exposing this[T1,T2] indexers referenced from compiled expressions.

Related errors


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/eee038afe50ef584. Report an issue: GitHub.