Jus*_*oob 2 c# dictionary unity-game-engine
我知道有很多这样的问题,但它们并不能解决我的问题。我很新,不太了解。
我的问题是我想从一个值中获取整个键和值。
我的字典
public Dictionary<string, (int Position, string Name, int Hp, int Power, int Agility)> Player1Inventory = new Dictionary<string, (int Position, string Name, int Hp, int Power, int Agility)>
{
["contentCube"] = (Position: 1, Name: "ContentCube", Hp: 200, Power: 100, Agility: 70),
};
Run Code Online (Sandbox Code Playgroud)
我需要根据他的职位获取所有信息。我在其他类似问题上发现使用 Linq,但他们给出的示例对我不起作用。
我发现但不起作用的示例:
var myKey = types.FirstOrDefault(x => x.Value == "one").Key;
Run Code Online (Sandbox Code Playgroud)
和
var keysWithMatchingValues = dic.Where(p => p.Value == "a").Select(p => p.Key);
Run Code Online (Sandbox Code Playgroud)
任何帮助或解释表示赞赏。
您几乎拥有它,但由于 Position 是一个 int,因此您无法在不先转换的情况下将其与字符串进行比较。您可以改为与 int 值进行比较:
Dictionary<string, (int Position, string Name, int Hp, int Power, int Agility)> Player1Inventory = new Dictionary<string, (int Position, string Name, int Hp, int Power, int Agility)>
{
["contentCube"] = (Position: 1, Name: "ContentCube", Hp: 200, Power: 100, Agility: 70),
};
var myKey = Player1Inventory.FirstOrDefault(x => x.Value.Position == 1).Key;
Console.WriteLine(myKey); // returns "contentCube"
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3720 次 |
最近记录: |