dotnet/efcore · error · NotImplementedException

IndexExpression with multiple arguments

Error message

IndexExpression with multiple arguments

What it means

Thrown by LinqToCSharpSyntaxTranslator.VisitIndex when an IndexExpression has more than one argument. The translator only renders single-argument element access (array[i], list[0], single-index indexer) via ElementAccessExpression with a bracketed argument list of one; multi-dimensional arrays (a[i,j]) and multi-parameter indexers are unimplemented.

Solutions

  1. Replace multi-argument indexing with an explicit method call to a helper that performs the lookup (the translator handles method calls).
  2. Flatten the multidimensional array into a single-dimensional one so the single-argument path applies.
  3. Wrap the multi-key access behind a public indexer-free API on a helper type and call it.

Example fix

// before — 2D array access via IndexExpression
var elem = Expression.ArrayAccess(
    matrixExpr,
    Expression.Constant(0),
    Expression.Constant(1)); // 2 args -> throws

// after — call a helper method
var elem = Expression.Call(
    matrixExpr,
    nameof(matrix.Get),
    Type.EmptyTypes,
    Expression.Constant(0),
    Expression.Constant(1));
Defensive patterns

Strategy: validation

Validate before calling

// Reject multi-argument IndexExpression nodes.
using System.Linq.Expressions;

bool HasMultiArgIndex(Expression e)
{
    bool bad = false;
    new IndexChecker(i => { if (i.Arguments.Count > 1) bad = true; }).Visit(e);
    return bad;
}

class IndexChecker(Action<IndexExpression> onIndex) : ExpressionVisitor
{
    protected override Expression VisitIndex(IndexExpression i) { onIndex(i); return base.VisitIndex(i); }
}

Type guard

static bool IsSingleArgIndex(IndexExpression i) => i.Arguments.Count <= 1;

Prevention

When it happens

Trigger: An expression tree contains an IndexExpression whose Arguments has 2+ entries — e.g. a 2D array access Expression.ArrayIndex(arr, i, j) (or Expression.Property with multiple index arguments on a multi-parameter indexer).

Common situations: Compiling expression trees that touch multidimensional arrays or multi-parameter indexers; provider extensions that surface multi-key lookups as index expressions; converting matrix-style data access into expression trees.

Related errors


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

Appendix: 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 3a2006ef56)