-1

I have a MultiValueDictionary<string, string> where I am trying to get a key by value.

var dic = na.prevNext; // Getter to get MultiValueDictionary

string nodePointingToThisOne = "";
foreach (var item in dic)
{
    if(item.Value == "test")
    {
        nodePointingToThisOne = item.Key;
    }
    break;
}

This does not work so I tried Linq:

string nodePointingToThisOne = dic.Where(x => x.Value == this.nodeID).Select(x => x.Key);

But on both I get this error: Operator '==' cannot be applied to operands of type 'System.Collections.Generic.IReadOnlyCollection<string>' and 'string'

So my question is how do I make this comparison work for a read-only collection? I am aware that I get problems if a key exists multiple times but I reduced the problem to this one for now.

I read Get Dictionary key by using the dictionary value

LINQ: Getting Keys for a given list of Values from Dictionary and vice versa

get dictionary key by value

Getting key of value of a generic Dictionary?

Get key from value - Dictionary<string, List<string>>

but they deal with a "normal" dictionary.

Community
  • 1
  • 1
Takeru
  • 219
  • 3
  • 11

5 Answers5

0

Since the Value property it self may contain multiple values you can't compare it directly against certain string using == operator. Use Contains() instead :

.....
if (item.Value.Contains("test"))
{
    .....
}

...or in method chain version :

string nodePointingToThisOne = dic.Where(x => x.Value.Contains("test"))
                                  .Select(x => x.Key)
                                  .FirstOrDefault();
har07
  • 88,338
  • 12
  • 84
  • 137
0

Try to iterate by Keys and then compare the value, and return the Key only if the Value matches.

Like this:

foreach (var key in dic.Keys)
{
    if(dic[key].Contains("your value"))
        return key;
}
Jordi
  • 1,460
  • 9
  • 9
0

You can iterate over keys like this

   foreach (var key in dic.Keys)
    {
        if(key == "your key")
            return key;
    }

You can also iterate over values like this

    foreach (var v in dic.Values)
    {
        if(v == "your value")
            return v;
    }

Example:

        Dictionary<string, string> c = new Dictionary<string, string>();
        c.Add("Pk", "Pakistan");
        c.Add("Aus", "Australia");
        c.Add("Ind", "India");
        c.Add("Nz", "New Zeland");
        c.Add("SA", "South Africa");

        foreach (var v in c.Values)
        {
            if (v == "Australia")
            {
                 Console.WriteLine("Your Value is = " + v);
                // perform your task 
            }
        }

        foreach (var k in c.Keys)
        {
            if (k == "Aus")
            {
                // perform your task    
                Console.WriteLine("Your Key is = " + k);
            }
        }

Output:

Your Value is = "Australia"
Your Key is = "Aus"

Khurram Ali
  • 1,659
  • 4
  • 20
  • 37
0

If you look at the MultiValueDictionary, line 81 will give you a hint. It is:

 [TestInitialize]
 public void TestInitialize()
 {
     MultiValueDictionary = new MultiValueDictionary<TestKey, string>();
 }

 protected static void AssertAreEqual( IDictionary<TestKey, string[]> expected,
 IMultiValueDictionary<TestKey, string> actual )
 {
     Assert.AreEqual( expected.Count, actual.Count );
     foreach ( var k in expected.Keys )
     {
         var expectedValues = expected[ k ];
         var actualValues = actual[ k ];
         AssertAreEqual( expectedValues, actualValues );
     }
 } 

So for your case, the solution is similar:

foreach (var item in dic.keys)
{
    if(dict[item] == "test")
    {
        nodePointingToThisOne = item;
        return nodePointingToThisOne;
    }
}
Hui Zhao
  • 655
  • 1
  • 10
  • 20
-1

worked for me:

foreach (int i in Dictionary.Keys)
{
   if (Dictionary.Values.ToString() == "Your Value")
   {
      return Dictionary.Keys;
   }
}
FranciscoNabas
  • 505
  • 3
  • 9