Re: [問題] 如何判斷Dictionary的key/value ?
※ 引述《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
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
C_Sharp 近期熱門文章
PTT數位生活區 即時熱門文章