【问题标题】:How to compare key/value dictionary with == operator on a IReadOnlyCollection<string>?如何在 IReadOnlyCollection<string> 上将键/值字典与 == 运算符进行比较?
【发布时间】:2015-05-23 08:34:45
【问题描述】:

我有一个MultiValueDictionary&lt;string, string&gt;,我正在尝试按值获取键。

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

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

这不起作用,所以我尝试了 Linq:

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

但在这两个上我都收到此错误:Operator '==' cannot be applied to operands of type 'System.Collections.Generic.IReadOnlyCollection&lt;string&gt;' and 'string'

所以我的问题是如何使这种比较适用于只读集合?我知道如果一个密钥存在多次,我会遇到问题,但我现在将问题减少到这个问题。

我读过 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>>

但他们处理的是“普通”字典。

【问题讨论】:

    标签: c# .net linq dictionary


    【解决方案1】:

    由于Value 属性本身可能包含多个值,因此您无法使用== 运算符直接将其与特定字符串进行比较。请改用Contains()

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

    ...或在方法链版本中:

    string nodePointingToThisOne = dic.Where(x => x.Value.Contains("test"))
                                      .Select(x => x.Key)
                                      .FirstOrDefault();
    

    【讨论】:

      【解决方案2】:

      尝试通过Keys进行迭代,然后比较value,只有Value匹配才返回Key。

      像这样:

      foreach (var key in dic.Keys)
      {
          if(dic[key].Contains("your value"))
              return key;
      }
      

      【讨论】:

        【解决方案3】:

        你可以像这样遍历键

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

        你也可以像这样迭代值

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

        示例:

                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);
                    }
                }
        

        输出:

        您的价值是 =“澳大利亚”
        你的钥匙是 = "Aus"

        【讨论】:

          【解决方案4】:

          如果您查看MultiValueDictionary, 第 81 行会给您一个提示。它是:

           [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 );
               }
           } 
          

          所以对于你的情况,解决方案是类似的:

          foreach (var item in dic.keys)
          {
              if(dict[item] == "test")
              {
                  nodePointingToThisOne = item;
                  return nodePointingToThisOne;
              }
          }
          

          【讨论】:

            【解决方案5】:

            为我工作:

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

            【讨论】:

              猜你喜欢
              • 2012-05-29
              • 2018-10-05
              • 2018-05-29
              • 1970-01-01
              • 2019-06-23
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-06-09
              相关资源
              最近更新 更多