根据值成员变量搜索 IDictionary
Search IDictionary based on value member variable
我有一个带有 KeyPair <int, SteamApp>
的 IDictionary,其中 SteamApp 是我正在使用的 SteamAPI 框架中的自定义 class。 SteamApp class 有一个字段 Name
,我想通过它来搜索字典。我想搜索特定的游戏名称。我该怎么做?
简短的代码片段:
foreach (var pair in allGames) {
Debug.WriteLine(pair.Value.Name);
}
所以您想要字典中所有 KeyValuePair<int, SteamApp>
中 SteamApp 名称与您正在搜索的名称相同的内容?
var allKeyValues = dict.Where(kv => kv.Value.Name == searchedName);
foreach(KeyValuePair<int, SteamApp> kv in allKeyValues)
{
// a game with that name exists in the dictionary
}
如果你只想拿第一个使用FirstOrDefault
:
var keyVal = dict.FirstOrDefault(kv => kv.Value.Name == searchedName);
if(keyVal.Value != null)
{
// a game with that name exists in the dictionary
}
我有一个带有 KeyPair <int, SteamApp>
的 IDictionary,其中 SteamApp 是我正在使用的 SteamAPI 框架中的自定义 class。 SteamApp class 有一个字段 Name
,我想通过它来搜索字典。我想搜索特定的游戏名称。我该怎么做?
简短的代码片段:
foreach (var pair in allGames) {
Debug.WriteLine(pair.Value.Name);
}
所以您想要字典中所有 KeyValuePair<int, SteamApp>
中 SteamApp 名称与您正在搜索的名称相同的内容?
var allKeyValues = dict.Where(kv => kv.Value.Name == searchedName);
foreach(KeyValuePair<int, SteamApp> kv in allKeyValues)
{
// a game with that name exists in the dictionary
}
如果你只想拿第一个使用FirstOrDefault
:
var keyVal = dict.FirstOrDefault(kv => kv.Value.Name == searchedName);
if(keyVal.Value != null)
{
// a game with that name exists in the dictionary
}