72

I have a Dictionary<string, string>.

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

ContainsValue just says true/false and not the index or key of that item.

Help!

Thanks

EDIT: Just found this - what do you think?

var key = (from k in dic where string.Compare(k.Value, "two", true) ==
0 select k.Key).FirstOrDefault();

EDIT 2: I also just knocked this up which might work

foreach (KeyValuePair<string, string> kvp in myDic)
{
    if (myList.Any(x => x.Id == kvp.Value))
        myDic.Remove(kvp.Key);
}
leppie
  • 115,091
  • 17
  • 196
  • 297
Jon
  • 38,814
  • 81
  • 233
  • 382
  • I think you need a BiDictionary. Check this question: http://stackoverflow.com/questions/255341/getting-key-of-value-of-a-generic-dictionary/255638#255638 – bruno conde Oct 28 '09 at 12:22
  • 5
    @Edit 2: you're going to remove things from a collection while enumerating it? Doesn't that throw an exception? – JustLoren Oct 28 '09 at 12:40
  • @JustLoren - Good Point! Will enumerate the list and run the LINQ to find the key if it exists and then just call remove from my dictionary without concern – Jon Oct 28 '09 at 12:46
  • 1
    @Jon, the foreach is fine as well, as long as you add a break after you've found and removed your key. – SoftMemes Oct 28 '09 at 14:01

6 Answers6

149

Are you trying to remove a single value or all matching values?

If you are trying to remove a single value, how do you define the value you wish to remove?

The reason you don't get a key back when querying on values is because the dictionary could contain multiple keys paired with the specified value.

If you wish to remove all matching instances of the same value, you can do this:

foreach(var item in dic.Where(kvp => kvp.Value == value).ToList())
{
    dic.Remove(item.Key);
}

And if you wish to remove the first matching instance, you can query to find the first item and just remove that:

var item = dic.First(kvp => kvp.Value == value);

dic.Remove(item.Key);

Note: The ToList() call is necessary to copy the values to a new collection. If the call is not made, the loop will be modifying the collection it is iterating over, causing an exception to be thrown on the next attempt to iterate after the first value is removed.

Paul Turner
  • 38,949
  • 15
  • 102
  • 166
  • 13
    First code example doesn't work, because its not allowed to modify collection in foreach loop. – kyrylomyr Dec 27 '11 at 08:28
  • An anonymous edit removed some of the necessary code. I'll put some of it back. – Paul Turner Dec 28 '11 at 09:35
  • 3
    In the second case, you'll get better performance by using this query: `foreach(var item in dic.Where(kvp => kvp.Value == value).Take(1).ToList())` and this also makes the `break` unnecessary. – cdhowie Sep 14 '12 at 20:13
  • 2
    That `ToList` trick is clever...it would have saved me from almost every `for` loop I've ever written. – Joel B Dec 11 '14 at 11:50
  • Thank you for your "Note" part :). – Jacob Apr 20 '22 at 13:09
9
Dictionary<string, string> source
//
//functional programming - do not modify state - only create new state
Dictionary<string, string> result = source
  .Where(kvp => string.Compare(kvp.Value, "two", true) != 0)
  .ToDictionary(kvp => kvp.Key, kvp => kvp.Value)
//
// or you could modify state
List<string> keys = source
  .Where(kvp => string.Compare(kvp.Value, "two", true) == 0)
  .Select(kvp => kvp.Key)
  .ToList();

foreach(string theKey in keys)
{
  source.Remove(theKey);
}
Amy B
  • 108,202
  • 21
  • 135
  • 185
1

Loop through the dictionary to find the index and then remove it.

bluish
  • 26,356
  • 27
  • 122
  • 180
Xinus
  • 29,617
  • 32
  • 119
  • 165
  • 2
    As noted in the accepted answer, if you remove a dictionary item while looping through the dictionary it will throw an error if there are values left in the dictionary after the one you removed. – ammills01 Jul 10 '17 at 15:53
1

Here is a method you can use:

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

You can use the following as extension method

 public static void RemoveByValue<T,T1>(this Dictionary<T,T1> src , T1 Value)
    {
        foreach (var item in src.Where(kvp => kvp.Value.Equals( Value)).ToList())
        {
            src.Remove(item.Key);
        }
    }
-2

In my case I use this

  var key=dict.FirstOrDefault(m => m.Value == s).Key;
            dict.Remove(key);