我将从 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"。
最佳答案
如果你想使用linq,你可以这样写:
var result = myDict.FirstOrDefault(p => p.Value.Contains(stringTofind)).Key;
关于c# - 按列表值获取字典键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61389663/