{"record":{"id":"0a7510d3df70d251","repo":"dotnet/efcore","slug":"this-enumerator-cannot-be-reset-0a7510","errorCode":null,"errorMessage":"This enumerator cannot be reset.","messagePattern":"This enumerator cannot be reset\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Query/Internal/GroupBySingleQueryingEnumerable.cs","lineNumber":369,"sourceCode":"\n            enumerator._relationalQueryContext.InitializeStateManager(enumerator._standAloneStateManager);\n\n            return false;\n        }\n\n        public void Dispose()\n        {\n            if (_dataReader is not null)\n            {\n                _relationalQueryContext.Connection.ReturnCommand(_relationalCommand!);\n                _dataReader.Dispose();\n                _dataReader = null;\n                _dbDataReader = null;\n            }\n        }\n\n        public void Reset()\n            => throw new NotSupportedException(CoreStrings.EnumerableResetNotSupported);\n    }\n\n    private sealed class AsyncEnumerator : IAsyncEnumerator<IGrouping<TKey, TElement>>\n    {\n        private readonly RelationalQueryContext _relationalQueryContext;\n        private readonly RelationalCommandResolver _relationalCommandResolver;\n        private readonly IReadOnlyList<ReaderColumn?>? _readerColumns;\n        private readonly Func<QueryContext, DbDataReader, TKey> _keySelector;\n        private readonly Func<QueryContext, DbDataReader, object[]> _keyIdentifier;\n        private readonly IReadOnlyList<Func<object, object, bool>> _keyIdentifierValueComparers;\n        private readonly Func<QueryContext, DbDataReader, ResultContext, SingleQueryResultCoordinator, TElement> _elementSelector;\n        private readonly Type _contextType;\n        private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _queryLogger;\n        private readonly bool _standAloneStateManager;\n        private readonly bool _detailedErrorsEnabled;\n        private readonly IConcurrencyDetector? _concurrencyDetector;\n        private readonly IExceptionDetector _exceptionDetector;\n        private readonly CancellationToken _cancellationToken;","sourceCodeStart":351,"sourceCodeEnd":387,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Query/Internal/GroupBySingleQueryingEnumerable.cs#L351-L387","documentation":"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.","triggerScenarios":"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.","commonSituations":"Custom LINQ operators that assume Reset semantics; third-party data-processing libraries applied to an IQueryable GroupBy; code migrated from LINQ-to-Objects.","solutions":["Materialize the grouping with ToList()/ToListAsync() before re-iterating; iterate the resulting List<IGrouping<...>>.","Re-run the IQueryable GroupBy to obtain a fresh enumerator (re-issues SQL).","Avoid manual enumerator manipulation; use foreach over the query directly.","If a library requires a resettable enumerator, feed it the buffered list instead of the live query."],"exampleFix":"// before\nvar grouped = context.Orders.GroupBy(o => o.CustomerId);\nusing var e = grouped.GetEnumerator();\ne.MoveNext();\ne.Reset(); // NotSupportedException\n\n// after\nvar grouped = context.Orders.GroupBy(o => o.CustomerId).ToList();\nusing var e = grouped.GetEnumerator(); // List enumerator, safe","handlingStrategy":"validation","validationCode":"// Buffer the GroupBy before re-iterating.\nvar groups = ctx.Set<T>().GroupBy(k => k.Key).ToList(); // safe to iterate multiple times","typeGuard":"static bool IsResettable<T>(IEnumerable<T> s) => s is IList<T> or Array;","tryCatchPattern":"try { e.Reset(); }\ncatch (NotSupportedException) {\n    var buffered = query.ToList(); // re-run and buffer\n}","preventionTips":["Never call Reset() on EF GroupBy enumerators; materialize first.","Use foreach over a ToList() result for repeated passes.","Pass buffered lists to reset-assuming helpers."],"tags":["ef-core","enumerator","reset","groupby","query"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}