{"record":{"id":"83f0f6ae32ab644d","repo":"dotnet/efcore","slug":"this-enumerator-cannot-be-reset-83f0f6","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.InMemory/Query/Internal/InMemoryShapedQueryCompilingExpressionVisitor.QueryingEnumerable.cs","lineNumber":164,"sourceCode":"                return hasNext;\n            }\n\n            public void Dispose()\n            {\n                _enumerator?.Dispose();\n                _enumerator = null;\n            }\n\n            public ValueTask DisposeAsync()\n            {\n                var enumerator = _enumerator;\n                _enumerator = null;\n\n                return enumerator.DisposeAsyncIfAvailable();\n            }\n\n            public void Reset()\n                => throw new NotSupportedException(CoreStrings.EnumerableResetNotSupported);\n        }\n    }\n}\n","sourceCodeStart":146,"sourceCodeEnd":168,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.InMemory/Query/Internal/InMemoryShapedQueryCompilingExpressionVisitor.QueryingEnumerable.cs#L146-L168","documentation":"The QueryingEnumerable enumerator returned by the InMemory shaped-query compiling expression visitor is forward-only. Its Reset() method unconditionally throws NotSupportedException with CoreStrings.EnumerableResetNotSupported. EF Core query result enumerators are designed for single-pass iteration and have no snapshot to rewind to.","triggerScenarios":"Calling IEnumerator.Reset() on the enumerator obtained by directly iterating an EF Core InMemory query (e.g. via ((IEnumerable<T>)query).GetEnumerator()). This is rare in normal foreach usage but can be triggered by libraries or frameworks that call Reset() on enumerators.","commonSituations":"Third-party libraries that reset enumerators (e.g. some reporting engines, data adapters, or UI binding frameworks). Manually managing an enumerator and trying to rewind it. Code that shares an enumerator across multiple iteration passes.","solutions":["Replace Reset() + re-iteration with re-executing the query: obtain a fresh enumerator (or re-call the query) for each pass.","Materialize results with ToList()/ToListAsync() if you need multiple iteration passes over the same data.","If a framework forces Reset(), project to a List first and enumerate the list instead of the raw query."],"exampleFix":"// before — manual enumerator with Reset\nusing var en = ((IEnumerable<Blog>)query).GetEnumerator();\nen.MoveNext();\n/* ... */\nen.Reset();  // throws NotSupportedException\nen.MoveNext();\n\n// after — materialize and iterate the list\nvar list = query.ToList();\nvar en = list.GetEnumerator();\nen.Reset();  // List<T>.Enumerator supports Reset","handlingStrategy":"validation","validationCode":"// Never call Reset() on an EF Core query enumerator.\n// If multiple passes are needed, materialize to a list first.\nvar materialized = query.ToList();\nvar enumerator = materialized.GetEnumerator();\n// List<T>.Enumerator.Reset() is supported; EF Core's is not.","typeGuard":"// Type guard: check if the enumerable is a materialized collection before Reset\nstatic bool CanReset<T>(IEnumerable<T> source)\n    => source is IList<T> or ICollection<T> or Array;\n\n// Usage:\nif (!CanReset(queryResult))\n    queryResult = queryResult.ToList();","tryCatchPattern":"try\n{\n    enumerator.Reset();\n}\ncatch (NotSupportedException ex) when (ex.Message.Contains(\"cannot be reset\"))\n{\n    // Re-execute the query or iterate a materialized copy\n    materialized = query.ToList();\n    enumerator = materialized.GetEnumerator();\n}","preventionTips":["Never call Reset() on an IEnumerator obtained from an EF Core query.","Materialize results with ToList()/ToListAsync() if multiple passes are needed.","When passing data to frameworks that call Reset(), convert to List<T> first.","Prefer foreach loops which never call Reset()."],"tags":["efcore","inmemory","enumerator","reset","forward-only"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}