提问人:Larry 提问时间:11/6/2021 更新时间:11/6/2021 访问量:61
C#:将字典与列表相交。匹配后返回字典项
C#: Intersect a dictionary with a list. Return dictionary item after match
问:
如何安全地返回与列表中的项目匹配的字典项目?
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"
}
答:
1赞
Caius Jard
11/6/2021
#1
因为字典的排列没有帮助,我想我可能会这样做:
var h = list.ToHashSet();
var result = dict.Where(kvp => h.Contains(kvp.Value));
下一个:检查数组中的目录名称
评论