Remove Item in Dictionary based on Value

前端 未结 6 2022
长情又很酷
长情又很酷 2020-11-30 05:37

I have a Dictionary.

I need to look within that dictionary to see if a value exists based on input from somewhere else and if it e

6条回答
  •  长情又很酷
    2020-11-30 06:00

    Here is a method you can use:

        public static void RemoveAllByValue(this Dictionary dictionary, V value)
        {
            foreach (var key in dictionary.Where(
                    kvp => EqualityComparer.Default.Equals(kvp.Value, value)).
                    Select(x => x.Key).ToArray())
                dictionary.Remove(key);
        }
    

提交回复
热议问题