I am using a StringDictionary to store key value pairs. I need to use the keys and values interchangeably i.e. I should be able to get the key from the value as in my case the values will be distinct.
我使用StringDictionary来存储键值对。我需要交替使用键和值,即我应该能够从值中获取键,因为在我的情况下,值将是不同的。
Is there a direct way I could do it (without looping)? Or if there is any other collection I could use to achieve this?
有没有直接的方法可以做到(没有循环)?或者,如果有任何其他收藏我可以用来实现这一目标?
Currently I am looping:
目前我正在循环:
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");
}
Any help is much appreciated. Thanks.
任何帮助深表感谢。谢谢。
4 个解决方案
#1
#2
The only way I can think of is two dictionaries.
我能想到的唯一方法是两本词典。
D1 < Key, Value > D2 < Value, Key >
D1
You would be repeating your data though. But you could search both keys and values this way.
你会重复你的数据。但是你可以用这种方式搜索键和值。
#3
The best idea is to use Dictionary instead of StringDictionary as its obselete.. you can find out more about this here: StringDictionary vs Dictionary<string, string> regarding retrival you should go with linq internally it is using loops but still its much cleaner way..
最好的想法是使用Dictionary而不是StringDictionary作为它的obselete ..你可以在这里找到更多相关信息:StringDictionary vs Dictionary
#4
This would also be possible using Dictionary:
这也可以使用Dictionary:
Dictionary<string, string> sd = new Dictionary<string, string>();
string sKey = sd.Single(kvp => kvp.Value.Equals("A value")).Key;
Note that the Single method will throw if the value occurs more than once. You could use First or even FirstOrDefault.
请注意,如果值多次出现,则抛出Single方法。您可以使用First或甚至FirstOrDefault。
Edit: As Jon Skeet commented this is still looping. The links he posted to a linked dictionary provide the solution.
编辑:正如Jon Skeet评论的那样,这仍然是循环的。他发布到链接词典的链接提供了解决方案。
#1
You basically need to use two dictionaries encapsulated into one.
你基本上需要使用两个字典封装成一个。
There are already implementations here and here in other Stack Overflow questions.
这里和其他Stack Overflow问题已经有了实现。
#2
The only way I can think of is two dictionaries.
我能想到的唯一方法是两本词典。
D1 < Key, Value > D2 < Value, Key >
D1
You would be repeating your data though. But you could search both keys and values this way.
你会重复你的数据。但是你可以用这种方式搜索键和值。
#3
The best idea is to use Dictionary instead of StringDictionary as its obselete.. you can find out more about this here: StringDictionary vs Dictionary<string, string> regarding retrival you should go with linq internally it is using loops but still its much cleaner way..
最好的想法是使用Dictionary而不是StringDictionary作为它的obselete ..你可以在这里找到更多相关信息:StringDictionary vs Dictionary
#4
This would also be possible using Dictionary:
这也可以使用Dictionary:
Dictionary<string, string> sd = new Dictionary<string, string>();
string sKey = sd.Single(kvp => kvp.Value.Equals("A value")).Key;
Note that the Single method will throw if the value occurs more than once. You could use First or even FirstOrDefault.
请注意,如果值多次出现,则抛出Single方法。您可以使用First或甚至FirstOrDefault。
Edit: As Jon Skeet commented this is still looping. The links he posted to a linked dictionary provide the solution.
编辑:正如Jon Skeet评论的那样,这仍然是循环的。他发布到链接词典的链接提供了解决方案。