c# - 按列表值获取字典键

标签 c# dictionary unity3d

我将从 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/

相关文章:

c# - 这个用于删除不在数组中的所有文件的 C# 例程看起来正常吗?

java - 如何通过将 map 对象传递给模型来检索 map 对象

c# - 从平面边缘 Unity 画线

C# 6.0 的新字典初始化器 - 说明

c# - 在 Unity 中保存/加载数据

c# - 找不到类型或命名空间名称 'UI'。 c#Unity3d

c# - DataContractJsonSerializer 人类可读的 json

c# - StreamWriter 不写

c# - 在 documentcompleted 事件之前禁用从 webbrowser 控件加载图像

python - 字典中具有多个值的键 (Python)