dotnet/efcore · error · NotSupportedException

This enumerator cannot be reset.

Error message

This enumerator cannot be reset.

What it means

Enumerator.Reset() in the InMemory QueryingEnumerable unconditionally throws NotSupportedException (CoreStrings.EnumerableResetNotSupported) (QueryingEnumerable.cs:163-164). EF Core query enumerators are single-pass; they cannot be rewound to the start.

Source

Thrown at src/EFCore.InMemory/Query/Internal/InMemoryShapedQueryCompilingExpressionVisitor.QueryingEnumerable.cs:164

                return hasNext;
            }

            public void Dispose()
            {
                _enumerator?.Dispose();
                _enumerator = null;
            }

            public ValueTask DisposeAsync()
            {
                var enumerator = _enumerator;
                _enumerator = null;

                return enumerator.DisposeAsyncIfAvailable();
            }

            public void Reset()
                => throw new NotSupportedException(CoreStrings.EnumerableResetNotSupported);
        }
    }
}

View on GitHub (pinned to dbf9771522)

Solutions

  1. Re-execute the query to obtain a fresh enumerator instead of calling Reset().
  2. Materialise results with ToList()/ToArray() and enumerate the in-memory collection multiple times.
  3. Wrap the query in a helper that returns a new enumerator on each request.

Example fix

// before
var e = query.GetEnumerator();
while (e.MoveNext()) { /* pass 1 */ }
e.Reset();
while (e.MoveNext()) { /* pass 2 */ }
// after
var list = query.ToList();
foreach (var x in list) { /* pass 1 */ }
foreach (var x in list) { /* pass 2 */ }
Defensive patterns

Strategy: validation

Validate before calling

// Never call Reset() on an EF query enumerator. Re-run the query or materialise:
var materialised = query.ToList(); // safe to enumerate multiple times

Prevention

When it happens

Trigger: Calling Reset() on the IEnumerator<T> or IAsyncEnumerator<T> obtained from an EF Core InMemory LINQ query. Some LINQ-to-Objects helpers or custom iteration code invoke Reset.

Common situations: Code that reuses an enumerator across multiple passes and calls Reset(), or third-party helpers that assume Reset() works. Rare in normal foreach usage.

Related errors


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