gpt4 book ai didi

collections - 从 .Net 集合中的值中获取键

转载 作者:行者123 更新时间:2023-12-02 07:55:59 26 4
gpt4 key购买 nike

我正在使用 StringDictionary 来存储键值对。我需要互换使用键和值,即我应该能够从值中获取键,因为在我的情况下,值将是不同的。

有没有一种直接的方法可以做到这一点(无需循环)?或者我是否可以使用任何其他集合来实现此目的?

目前我正在循环:

public String GetKeyFromValue(string value)
{
foreach (KeyValuePair<string, string> kvp in instance)
{
if (String.Equals(kvp.Value, value))
return kvp.Key;
}

throw new Exception ("Key not found in FilterControlMapping");
}

非常感谢任何帮助。谢谢。

最佳答案

您基本上需要将两个词典封装成一个。

已经有实现 herehere在其他 Stack Overflow 问题中。

关于collections - 从 .Net 集合中的值中获取键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/867126/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com