{"record":{"id":"d45dd797267c5a3b","repo":"dotnet/wpf","slug":"sr-format-sr-implementothermemberswithsort-getenumerator","errorCode":null,"errorMessage":"SR.Format(SR.ImplementOtherMembersWithSort, \"GetEnumerator()\")","messagePattern":"SR\\.Format\\(SR\\.ImplementOtherMembersWithSort, \"GetEnumerator\\(\\)\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/CollectionView.cs","lineNumber":892,"sourceCode":"            if (IsCurrentBeforeFirst != oldIsCurrentBeforeFirst)\n                OnPropertyChanged(IsCurrentBeforeFirstPropertyName);\n\n            if (oldCurrentPosition != CurrentPosition)\n                OnPropertyChanged(CurrentPositionPropertyName);\n\n            if (oldCurrentItem != CurrentItem)\n                OnPropertyChanged(CurrentItemPropertyName);\n        }\n\n        /// <summary>\n        /// Returns an object that enumerates the items in this view.\n        /// </summary>\n        protected virtual IEnumerator GetEnumerator()\n        {\n            VerifyRefreshNotDeferred();\n\n            if (SortDescriptions.Count > 0)\n                throw new InvalidOperationException(SR.Format(SR.ImplementOtherMembersWithSort, \"GetEnumerator()\"));\n\n            return EnumerableWrapper.GetEnumerator();\n        }\n\n\n        /// <summary>\n        ///     Notify listeners that this View has changed\n        /// </summary>\n        /// <remarks>\n        ///     CollectionViews (and sub-classes) should take their filter/sort/grouping\n        ///     into account before calling this method to forward CollectionChanged events.\n        /// </remarks>\n        /// <param name=\"args\">\n        ///     The NotifyCollectionChangedEventArgs to be passed to the EventHandler\n        /// </param>\n        protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs args)\n        {\n            ArgumentNullException.ThrowIfNull(args);","sourceCodeStart":874,"sourceCodeEnd":910,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/CollectionView.cs#L874-L910","documentation":"The protected GetEnumerator of base CollectionView throws InvalidOperationException when SortDescriptions is non-empty, because the base implementation can only enumerate unsorted data. Sorted enumeration requires a derived view (e.g. ListCollectionView) that overrides GetEnumerator.","triggerScenarios":"Enumerating a plain CollectionView (foreach, LINQ, ItemsSource iteration) after SortDescriptions were added to it; the base GetEnumerator path is hit because the derived class doesn't override sorting.","commonSituations":"Iterating the view in code while sort descriptions are set on a base CollectionView; tests or helpers that foreach over CollectionView directly; custom CollectionView subclasses that forgot to override GetEnumerator/RefreshOverride for sorting.","solutions":["Use ListCollectionView or another sorting-capable view when SortDescriptions are set","Clear the SortDescriptions before enumerating the base view","Enumerate the underlying collection directly if sorting isn't needed","In a custom CollectionView, override GetEnumerator (and RefreshOverride) to apply sorting"],"exampleFix":"// before\nview.SortDescriptions.Add(sd);\nforeach (var item in view) { } // base GetEnumerator throws\n// after\nvar lcv = view as ListCollectionView ?? new ListCollectionView((IList)source);\nlcv.SortDescriptions.Add(sd);\nforeach (var item in lcv) { }","handlingStrategy":"validation","validationCode":"if (view.SortDescriptions.Count > 0)\n    items = view.Cast<object>().OrderBy(/* sort props */).ToList(); // snapshot instead of enumerating view\nelse\n    items = view.Cast<object>().ToList();","typeGuard":"bool canEnumerateSorted = view is ListCollectionView || view.SortDescriptions.Count == 0;","tryCatchPattern":"try { foreach (var i in view) Process(i); }\ncatch (InvalidOperationException) { foreach (var i in view.SourceCollection) Process(i); }","preventionTips":["Enumerate the underlying SourceCollection when sorting is active on a base CollectionView","Snapshot (ToList) before iterating across async boundaries","Prefer ListCollectionView for sorted views"],"tags":["wpf","collectionview","enumeration","sorting"],"backgroundTag":"method-not-implemented","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}