一般比較常見的情況有:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System.Linq;
- public class XXX : MonoBehaviour {
- public struct playerDataStruct
- {
- public string nickName;
- public int age;
- public int score;
- }
- private Dictionary<string, playerDataStruct> playerDataDictionary = new Dictionary<string, playerDataStruct>();
- private playerDataStruct tempPlayerDataStruct = new playerDataStruct();
- void Start ()
- {
- tempPlayerDataStruct.nickName = "Warrior";
- tempPlayerDataStruct.age = 18;
- tempPlayerDataStruct.score = 1000;
- playerDataDictionary.Add("John", tempPlayerDataStruct);
- tempPlayerDataStruct.nickName = "Fighter";
- tempPlayerDataStruct.age = 20;
- tempPlayerDataStruct.score = 2000;
- playerDataDictionary.Add("Peter", tempPlayerDataStruct);
- //----------------------------------------------------------------------------------------------------------------
- if (playerDataDictionary.Values.Any(x => x.age >= 20) == true)
- {
- Debug.Log("本遊戲有大於20歲以上的玩家。");
- }
- Debug.Log("本遊戲內小於20歲的玩家第一人為:" + playerDataDictionary.Where(x => x.Value.age < 20).Select(x => x.Key).FirstOrDefault());
- }
- void Update ()
- {
- }
- }
1.使用Value去逆向尋找Key。
2.判斷此Dictionary內是否包含該Key或是該Value。
3.直接尋找或比對Structure內的某一個屬性。
自己所知的對應方式:
依據條件把找到Key的全都取出
playerDataDictionary.Where(x => x.Value.age < 20).Select(x => x.Key)
取出找到的第一個Key
playerDataDictionary.Where(x => x.Value.age > 20).Select(x => x.Key).FirstOrDefault())
playerDataDictionary.FirstOrDefault(x => x.age >= 20).Key
判斷有無該Key包含在內
playerDataDictionary.ContainsKey("Peter")
判斷有無該Value的屬性包含在內
playerDataDictionary.Values.Any(x => x.age >= 20)
沒有留言:
張貼留言