Repository: AvaloniaUI/Avalonia
File: src/Avalonia.Controls/VirtualizingPanel.cs (C#)

Code before:
        /// <param name="index">
        /// The beginning index position within the collection at which the first child element is
        /// removed.
        /// </param>
        /// <param name="count">The number of child elements to remove.</param>
        protected void RemoveInternalChildRange(int index, int count)
        {
            var itemsControl = EnsureItemsControl();
            
            for (var i = index; i < count; ++i)
            {
                var c = Children[i];
                itemsControl.RemoveLogicalChild(c);
            }

            Children.RemoveRange(index, count);
        }

        /// <summary>
        /// Gets the index of a content control item contained within this panel that matches the given text search
        /// </summary>
        /// <param name="textSearchTerm">The beginning of a string (case-insensitive) to search for in the panel</param>
        /// <returns>The index of the first IContentControl item contained in the panel</returns>
        protected internal int GetIndexFromTextSearch(string textSearchTerm)
        {
            var matchingControl = Items.FirstOrDefault(i => i is Control c && ItemsPresenter.ControlMatchesTextSearch(c, textSearchTerm));
            if (matchingControl != null)
            {
                return Items.IndexOf(matchingControl);
            }

            var matchingObject = Items.FirstOrDefault(i =>
                i?.ToString()?.StartsWith(textSearchTerm, StringComparison.OrdinalIgnoreCase) == true);
            return matchingObject != null ? Items.IndexOf(matchingObject) : -1;
        }

        private protected override void InvalidateMeasureOnChildrenChanged()
        {
            // Don't invalidate measure when children are added or removed: the panel is responsible
            // for managing its children.
        }

        internal void Attach(ItemsControl itemsControl)
        {
            if (ItemsControl is not null)
                throw new InvalidOperationException("The VirtualizingPanel is already attached to an ItemsControl");

            ItemsControl = itemsControl;
            ItemsControl.ItemsView.PostCollectionChanged += OnItemsControlItemsChanged;
        }


Review comment (suggestion):
Use a single loop, and check each item for `ControlMatchesText` then `ToString()`, keeping the old behavior.

Code after:
        /// The beginning index position within the collection at which the first child element is
        /// removed.
        /// </param>
        /// <param name="count">The number of child elements to remove.</param>
        protected void RemoveInternalChildRange(int index, int count)
        {
            var itemsControl = EnsureItemsControl();
            
            for (var i = index; i < count; ++i)
            {
                var c = Children[i];
                itemsControl.RemoveLogicalChild(c);
            }

            Children.RemoveRange(index, count);
        }

        internal int GetIndexFromTextSearch(string textSearchTerm)
        {
            return ItemsPresenter.GetIndexFromTextSearch(Items, textSearchTerm);
        }

        private protected override void InvalidateMeasureOnChildrenChanged()
        {
            // Don't invalidate measure when children are added or removed: the panel is responsible
            // for managing its children.
        }

        internal void Attach(ItemsControl itemsControl)
        {
            if (ItemsControl is not null)
                throw new InvalidOperationException("The VirtualizingPanel is already attached to an ItemsControl");

            ItemsControl = itemsControl;
            ItemsControl.ItemsView.PostCollectionChanged += OnItemsControlItemsChanged;
        }

        internal void Detach()
        {
            var itemsControl = EnsureItemsControl();
            itemsControl.ItemsView.PostCollectionChanged -= OnItemsControlItemsChanged;
            ItemsControl = null;
            Children.Clear();
        }

        internal void Refresh() => OnItemsControlItemsChanged(null, CollectionUtils.ResetEventArgs);

        private ItemsControl EnsureItemsControl()
        {
            if (ItemsControl is null)
                ThrowNotAttached();