如何安全回傳與串列中的專案匹配的字典專案?
static void Test()
{
var dict = new Dictionary<string, string>();
dict.Add("license1", "123");
dict.Add("license2", "456");
dict.Add("license3", "789");
var list = new List<string>();
list.Add("444");
list.Add("111");
list.Add("123");
var result = dict.Values.Intersect(list);
//result should be only the matching item as a dictionary for dict -> for this example = "license1, 123"
}
uj5u.com熱心網友回復:
因為字典的排列沒有幫助,我想我可能會這樣做:
var h = list.ToHashSet();
var result = dict.Where(kvp => h.Contains(kvp.Value));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/350199.html
上一篇:重建字典串列
