gpt4 book ai didi

c# - 如何从 IDictionary 中获取键值对?

转载 作者:太空宇宙 更新时间:2023-11-03 17:49:48 25 4
gpt4 key购买 nike

我有一个json文件,其中包含一些像这样的值:

{AvailableWeapons:[ {"LevelNumber":1,"WeaponName":"Axe"},{"LevelNumber":5,"WeaponName":"WarHammer"} ]

使用 miniJSon,我将其解析为 IDictionary,如下所示:
IDictionary weaponsGrid = JsonManager.instance.availableWeapons as IDictionary;

我在一个具有整数参数“级别”的方法中访问它。该方法采用参数,将其与每个级别编号字段进行比较。如果 level 大于 levelNumber,则相应的武器可用。

伪代码:
foreach levelNumber in weaponsGrid,
if currentLevel > levelNumber
get name of weapon available at that level

我怎样才能在 C# 中完成这个?谢谢

最佳答案

使用 linq:

IDictionary<int, string> dict = new Dictionary<int, string> { { 1, "Axe" }, { 6, "WarHammer" }, { 2, "Knife" }, { 9, "Gun" } };

var list = dict.Where(x => x.Key > 5).Select(x=> x.Value)
.ToList();

这为您提供了一个值列表,其中键大于 X

关于c# - 如何从 IDictionary 中获取键值对?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34634304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com