{"record":{"id":"f423f75ea04464ac","repo":"dotnet/efcore","slug":"this-enumerator-cannot-be-reset","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.Cosmos/Query/Internal/CosmosShapedQueryCompilingExpressionVisitor.ReadItemQueryingEnumerable.cs","lineNumber":186,"sourceCode":"                    else\n                    {\n                        _queryLogger.QueryIterationFailed(_contextType, exception);\n                    }\n\n                    throw;\n                }\n            }\n\n            public ValueTask DisposeAsync()\n            {\n                _response = null;\n                _hasExecuted = false;\n\n                return default;\n            }\n\n            public void Reset()\n                => throw new NotSupportedException(CoreStrings.EnumerableResetNotSupported);\n\n            private bool ShapeResult()\n            {\n                var hasNext = _response is not null;\n\n                _cosmosQueryContext.InitializeStateManager(_standAloneStateManager);\n\n                Current\n                    = hasNext\n                        ? _shaper(_cosmosQueryContext, _response.Value, ordinal: 0, out _)\n                        : default;\n\n                _hasExecuted = true;\n\n                return hasNext;\n            }\n        }\n    }","sourceCodeStart":168,"sourceCodeEnd":204,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Query/Internal/CosmosShapedQueryCompilingExpressionVisitor.ReadItemQueryingEnumerable.cs#L168-L204","documentation":"The AsyncEnumerator implements IEnumerator/IEnumerator<T>.Reset explicitly and throws NotSupportedException (CoreStrings.EnumerableResetNotSupported) — EF's async enumerators are forward-only because Cosmos streams cannot rewind. Compiler-generated foreach reset logic or explicit Reset() calls trip this.","triggerScenarios":"Calling .Reset() on the enumerator obtained from a Cosmos query, or using an API that internally resets enumerators (some grid/binding components, .NET's Enumerable.Take on a reset-capable wrapper).","commonSituations":"UI data binding that re-enumerates the same source, third-party batching helpers that call Reset between passes, or migrating from List-backed sources to IQueryable.","solutions":["Re-execute the query to 'reset' — do not reuse the enumerator.","Materialize results into a List<T> with ToListAsync if you need multiple enumerations.","Avoid APIs that contractually require IEnumerator.Reset."],"exampleFix":"// before\nvar e = ((IEnumerable<User>)query).GetEnumerator();\nwhile (e.MoveNext()) { /*...*/ }\ne.Reset();\n// after\nvar list = await query.ToListAsync();\nforeach (var u in list) { /*...*/ }","handlingStrategy":"try-catch","validationCode":"static async Task<List<T>> MaterializeAsync<T>(IQueryable<T> q, CancellationToken ct)\n    where T : class\n    => await q.ToListAsync(ct).ConfigureAwait(false);  // never reuse enumerators","typeGuard":null,"tryCatchPattern":"try\n{\n    enumerator.Reset();\n}\ncatch (NotSupportedException ex) when (ex.Message.Contains(\"cannot be reset\"))\n{\n    // re-issue the query instead\n}","preventionTips":["Materialize to List<T> if multiple passes are needed.","Never call Reset on an EF async enumerator.","Avoid UI binding components that rely on Reset."],"tags":["cosmos","enumeration","async","reset"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}