Remove from ICollection by condition - CSharp System.Collections

CSharp examples for System.Collections:ICollection

Description

Remove from ICollection by condition

Demo Code

// of the GNU Public License, verison 2.
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//from   w ww  . j  a  v  a 2  s. co m

public class Main{
        public static void RemoveIf<T>(this ICollection<T> collection, Func<T, bool> predicate)
        {
            var elements = collection.Where(predicate).ToList();
            foreach(var element in elements) collection.Remove(element);
        }
}

Related Tutorials