【发布时间】:2020-08-06 22:08:09
【问题描述】:
我将从this previous question 获得灵感。我有一本字典,里面有列表,我想通过其中一个值来获取键。
Dictionary<string, List<string>> myDict = new Dictionary<string, List<string>>
{
{"1", new List<string>{"1a", "1b"} },
{"2", new List<string>{"2a", "2b"} },
{"3", new List<string>{"3a", "3b"} },
};
我相信里面的所有值都是独一无二的。
我想要这样的东西:
getByValueKey(字符串值);
getByValueKey("2a") 必须返回 "2"。
【问题讨论】:
-
我认为您要么必须遍历所有条目才能找到它,要么使用此字典构建反向映射字典并使用它。这与您链接的问题几乎相同,除了您可以使用 .Contains("2a") not == "2a" 测试值
-
谢谢!我想知道是否有类似于 Kimi 发布的单行解决方案:
var myKey = types.FirstOrDefault(x => x.Value == "one").Key;
标签: c# dictionary unity3d