Does the <T> .RemoveAll list not exist in Silverlight?

I am working on a Silverlight 2/3 application. I would like to use List.RemoveAll (or maybe IList.RemoveAll?) And specify a predicate so that I can remove a bunch of elements from the list in one sweep.

This feature does not seem to exist in Silverlight. Am I missing something? Is there an alternative approach that is equally simple? Right now, I manually iterate over my elements in foreach and save the second list (because you cannot delete during iteration), and that is pretty ... cumbersome.

+3
source share
3 answers

If you really need access to a subset, then there is no reason to delete it, just access this subset as follows:

Instead (maybe:

List<string> subSet = l.RemoveAll ( p => !p.StartsWith ("a") );

Just get the opposite:

List<string> l = new List<string> () { "a", "b", "aa", "ab" };
var subSet = l.Where ( p => p.StartsWith ( "a" ) );

<h / "> OK, but really delete them (subject to the same start list as above):

l.Where ( p => p.StartsWith ( "a" ) ).ToList ().ForEach ( q => l.Remove ( q ) );

. There is an extension method in IEnumerable, in System.Linq. As long as your list is general IEnumerable (and you added its use), it should be available.

+3
source

You can use LINQ, for example:

list = list.Where(predicate).ToList();

An alternative approach is to remove elements in a for loop:

for (int i = list.Count - 1; i >= 0; --i)
    if (predicate(list[i])) 
         list.RemoveAt(i);
+3
source

, . , :

    /// <summary>
    /// Removes all entries from a target list where the predicate is true.
    /// </summary>
    /// <typeparam name="T">The type of item that must exist in the list.</typeparam>
    /// <param name="list">The list to remove entries from</param>
    /// <param name="predicate">The predicate that contains a testing criteria to determine if an entry should be removed from the list.</param>
    /// <returns>The number of records removed.</returns>
    public static int RemoveAll<T>(this IList<T> list, Predicate<T> predicate)
    {
        int returnCount = 0;

        for (int i = list.Count - 1; i >= 0; --i)
        {
            if (predicate(list[i]))
            {
                list.RemoveAt(i);
                returnCount++;
            }
        }

        return returnCount;
    }
+2

Source: https://habr.com/ru/post/1729937/


All Articles