Re: [問題] 如何判斷Dictionary的key/value ?

看板C_Sharp (C#)作者 (阿里山 我來了XD~)時間10年前 (2015/03/06 23:54), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串2/2 (看更多)
※ 引述《don750421 (1+1≠2)》之銘言: : 假設我有個Dictionary如下 : Dictionary<string, sring> dict = new Dictionary<string, string>(); : dict.Add("apple", "30"); : dict.Add("banana", "50"); : dict.Add("grape", "100"); : 假設我有個Patten 陣列,裡面裝的都是代表"一定"要出現的值 : string[] Patten = new string[]{"apple","orange"}; : 因此 : 我要怎麼判斷,Patten是否有完全出現在Dictionary中呢? : 如果Dictionary沒有包含Patten的Key值及Key所對應的值為空則出現錯誤 : 除了用迴圈外,還有其他方式嗎?(LINQ?Lamdba?) : 謝謝 var dict = new Dictionary<string, string> { {"apple", "30"}, {"banana", "50"}, {"grape", "100"} }; string[] patten = {"apple", "orange"}; string value; var result = patten.All(str => dict.TryGetValue(str, out value) && !string.IsNullOrEmpty(value)); result 應該會是false -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.165.27.77 ※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1425657276.A.802.html

03/07 07:44, , 1F
感謝前輩,來試試看
03/07 07:44, 1F
文章代碼(AID): #1K-SsyW2 (C_Sharp)
討論串 (同標題文章)
文章代碼(AID): #1K-SsyW2 (C_Sharp)