dotnet/wpf · error · InvalidOperationException

SR.Format(SR.ImplementOtherMembersWithSort…

Error message

SR.Format(SR.ImplementOtherMembersWithSort, "GetEnumerator()")

What it means

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.

Solutions

  1. Use ListCollectionView or another sorting-capable view when SortDescriptions are set
  2. Clear the SortDescriptions before enumerating the base view
  3. Enumerate the underlying collection directly if sorting isn't needed
  4. In a custom CollectionView, override GetEnumerator (and RefreshOverride) to apply sorting

Example fix

// before
view.SortDescriptions.Add(sd);
foreach (var item in view) { } // base GetEnumerator throws
// after
var lcv = view as ListCollectionView ?? new ListCollectionView((IList)source);
lcv.SortDescriptions.Add(sd);
foreach (var item in lcv) { }
Defensive patterns

Strategy: validation

Validate before calling

if (view.SortDescriptions.Count > 0)
    items = view.Cast<object>().OrderBy(/* sort props */).ToList(); // snapshot instead of enumerating view
else
    items = view.Cast<object>().ToList();

Type guard

bool canEnumerateSorted = view is ListCollectionView || view.SortDescriptions.Count == 0;

Try / catch

try { foreach (var i in view) Process(i); }
catch (InvalidOperationException) { foreach (var i in view.SourceCollection) Process(i); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/d45dd797267c5a3b. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/CollectionView.cs:892

            if (IsCurrentBeforeFirst != oldIsCurrentBeforeFirst)
                OnPropertyChanged(IsCurrentBeforeFirstPropertyName);

            if (oldCurrentPosition != CurrentPosition)
                OnPropertyChanged(CurrentPositionPropertyName);

            if (oldCurrentItem != CurrentItem)
                OnPropertyChanged(CurrentItemPropertyName);
        }

        /// <summary>
        /// Returns an object that enumerates the items in this view.
        /// </summary>
        protected virtual IEnumerator GetEnumerator()
        {
            VerifyRefreshNotDeferred();

            if (SortDescriptions.Count > 0)
                throw new InvalidOperationException(SR.Format(SR.ImplementOtherMembersWithSort, "GetEnumerator()"));

            return EnumerableWrapper.GetEnumerator();
        }


        /// <summary>
        ///     Notify listeners that this View has changed
        /// </summary>
        /// <remarks>
        ///     CollectionViews (and sub-classes) should take their filter/sort/grouping
        ///     into account before calling this method to forward CollectionChanged events.
        /// </remarks>
        /// <param name="args">
        ///     The NotifyCollectionChangedEventArgs to be passed to the EventHandler
        /// </param>
        protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs args)
        {
            ArgumentNullException.ThrowIfNull(args);

View on GitHub (pinned to 81131a70a4)