The most reliable way to clear the <T> list depending on the Predicate <T>

I have List<T> dataandPredicate<T> condition.

How should I use for cleaning datadepending on the result condition?

  • Option 1:

    var del = data.Where(i => condition);
    data.RemoveAll(i => del.Contains(i));
    
  • Option 2:

    var del = data.Where(i => condition);
    for (int i = 0; i < del.Count; i++)
        data.Remove(del[i]);
    
  • Option 3:

    var del = data.Where(i => condition);
    foreach (var i in del)
       data.Remove(i);
    
  • Option 4:

    data = data.Where(i => !condition);
    
  • Any others?

+3
source share
1 answer

What about:

data.RemoveAll(condition);

Please note that your fourth option will not work without a call ToList().

+10
source

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


All Articles