dotnet/efcore · error · NotSupportedException

This enumerator cannot be reset.

Error message

This enumerator cannot be reset.

What it means

Thrown from Enumerator.Reset() on GroupBySplitQueryingEnumerable (GroupBySplitQueryingEnumerable.cs:383). This enumerator drives a split-query GroupBy that issues separate SQL per related collection, coordinating multiple forward-only readers; it cannot be rewound. Reset is unsupported by design.

Source

Thrown at src/EFCore.Relational/Query/Internal/GroupBySplitQueryingEnumerable.cs:383

                if (_resultCoordinator != null)
                {
                    foreach (var dataReader in _resultCoordinator.DataReaders)
                    {
                        dataReader?.DataReader.Dispose();
                    }

                    _resultCoordinator.DataReaders.Clear();

                    _resultCoordinator = null;
                }

                _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, SplitQueryResultCoordinator, TElement> _elementSelector;
        private readonly Func<QueryContext, IExecutionStrategy, SplitQueryResultCoordinator, Task>? _relatedDataLoaders;
        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;

View on GitHub (pinned to dbf9771522)

Solutions

  1. Buffer results with ToList()/ToListAsync() and iterate the in-memory list.
  2. Re-execute the IQueryable to get a fresh enumerator.
  3. Avoid direct enumerator Reset calls; prefer foreach.
  4. When a third-party component needs resettable enumeration, pass the materialized list, not the EF enumerable.

Example fix

// before
var grouped = context.Customers
    .AsSplitQuery()
    .GroupBy(c => c.Region);
using var e = grouped.GetEnumerator();
e.Reset(); // throws

// after
var grouped = context.Customers
    .AsSplitQuery()
    .GroupBy(c => c.Region)
    .ToList();
using var e = grouped.GetEnumerator(); // safe, in-memory
Defensive patterns

Strategy: validation

Validate before calling

var groups = ctx.Set<T>().AsSplitQuery().GroupBy(k => k.Key).ToList(); // buffer for reuse

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();
}

Prevention

When it happens

Trigger: Calling IEnumerator.Reset() on an enumerator from a GroupBy query running with AsSplitQuery() (or the global UseSplitQueries). Triggered by code or libraries that re-iterate via Reset instead of re-querying.

Common situations: Using AsSplitQuery() for a GroupBy with includes and then passing the live enumerable to a reset-assuming helper; migrated LINQ-to-Objects code; custom aggregation helpers.

Related errors


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