How to delete an item from a dictionary using LINQ in C #

advertisements

I need to delete a specific item from a dictonary..

The dictonary is like

      dict["Key1"]="Value1"
      dict["Key2"]="Value2"
      dict["Key3"]="Value3"
      dict["Key4"]="Value2"

How to delete the item if another item has the same value using LINQ

Thanks in advance


Here my tested solution:

dict.GroupBy(x => x.Value, x => x.Key)
.Where(x => x.Count() > 1)
.SelectMany(x => x.Skip(1))
.ToList().ForEach(x => dict.Remove(x))