dotnet/efcore · error · NotSupportedException

This enumerator cannot be reset.

Error message

This enumerator cannot be reset.

What it means

Thrown from Enumerator.Reset() on GroupBySingleQueryingEnumerable (GroupBySingleQueryingEnumerable.cs:369). This enumerator drives a single-query (non-split) GroupBy materialization over a forward-only DbDataReader and accumulates groups in memory as it streams; it cannot be rewound. Reset is unsupported by design, identical to all relational EF enumerators.

Source

Thrown at src/EFCore.Relational/Query/Internal/GroupBySingleQueryingEnumerable.cs:369

            enumerator._relationalQueryContext.InitializeStateManager(enumerator._standAloneStateManager);

            return false;
        }

        public void Dispose()
        {
            if (_dataReader is not null)
            {
                _relationalQueryContext.Connection.ReturnCommand(_relationalCommand!);
                _dataReader.Dispose();
                _dataReader = null;
                _dbDataReader = null;
            }
        }

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

    private sealed class AsyncEnumerator : IAsyncEnumerator<IGrouping<TKey, TElement>>
    {
        private readonly RelationalQueryContext _relationalQueryContext;
        private readonly RelationalCommandResolver _relationalCommandResolver;
        private readonly IReadOnlyList<ReaderColumn?>? _readerColumns;
        private readonly Func<QueryContext, DbDataReader, TKey> _keySelector;
        private readonly Func<QueryContext, DbDataReader, object[]> _keyIdentifier;
        private readonly IReadOnlyList<Func<object, object, bool>> _keyIdentifierValueComparers;
        private readonly Func<QueryContext, DbDataReader, ResultContext, SingleQueryResultCoordinator, TElement> _elementSelector;
        private readonly Type _contextType;
        private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _queryLogger;
        private readonly bool _standAloneStateManager;
        private readonly bool _detailedErrorsEnabled;
        private readonly IConcurrencyDetector? _concurrencyDetector;
        private readonly IExceptionDetector _exceptionDetector;
        private readonly CancellationToken _cancellationToken;

View on GitHub (pinned to dbf9771522)

Solutions

  1. Materialize the grouping with ToList()/ToListAsync() before re-iterating; iterate the resulting List<IGrouping<...>>.
  2. Re-run the IQueryable GroupBy to obtain a fresh enumerator (re-issues SQL).
  3. Avoid manual enumerator manipulation; use foreach over the query directly.
  4. If a library requires a resettable enumerator, feed it the buffered list instead of the live query.

Example fix

// before
var grouped = context.Orders.GroupBy(o => o.CustomerId);
using var e = grouped.GetEnumerator();
e.MoveNext();
e.Reset(); // NotSupportedException

// after
var grouped = context.Orders.GroupBy(o => o.CustomerId).ToList();
using var e = grouped.GetEnumerator(); // List enumerator, safe
Defensive patterns

Strategy: validation

Validate before calling

// Buffer the GroupBy before re-iterating.
var groups = ctx.Set<T>().GroupBy(k => k.Key).ToList(); // safe to iterate multiple times

Type guard

static bool IsResettable<T>(IEnumerable<T> s) => s is IList<T> or Array;

Try / catch

try { e.Reset(); }
catch (NotSupportedException) {
    var buffered = query.ToList(); // re-run and buffer
}

Prevention

When it happens

Trigger: Calling IEnumerator.Reset() on an enumerator obtained from an EF GroupBy query that uses the single-query strategy (default). Triggered by libraries or helpers that re-iterate enumerators via Reset, or by manually holding an enumerator and reusing it.

Common situations: Custom LINQ operators that assume Reset semantics; third-party data-processing libraries applied to an IQueryable GroupBy; code migrated from LINQ-to-Objects.

Related errors


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