Remove all dictionary elements matching the given predicate. - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IDictionary

Description

Remove all dictionary elements matching the given predicate.

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//from w  w w  .  j a va  2 s.com

public class Main{
        /// <summary>
        /// Remove all dictionary elements matching the given predicate.
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="This"></param>
        /// <param name="Predicate"></param>
        public static void RemoveAll<TKey, TValue>(this IDictionary<TKey, TValue> This,
            Func<KeyValuePair<TKey, TValue>, bool> Predicate)
        {
            var Keys = This.Where(i => Predicate(i)).ToList();
            foreach (var i in Keys)
                This.Remove(i.Key);
        }
}

Related Tutorials