.net 如何比较键/值字典和IReadOnlyCollection上的==运算符< string>?

ncecgwcz  于 2023-03-04  发布在  .NET
关注(0)|答案(5)|浏览(94)

我有一个MultiValueDictionary<string, string>,我试图通过值获取一个键。

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<string>' 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>
但它们处理的是“普通”字典。

jobtbby3

jobtbby31#

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

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

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

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

kqhtkvqz2#

尝试按Keys进行迭代,然后比较值,仅当Value匹配时才返回Key。
就像这样:

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

iqjalb3h3#

可以像这样迭代键

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"

bxfogqkk

bxfogqkk4#

如果你看一下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;
    }
}
plicqrtu

plicqrtu5#

为我工作:

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

相关问题