【问题标题】:C#: Intersect a dictionary with a list. Return dictionary item after matchC#:将字典与列表相交。匹配后返回字典项
【发布时间】:2021-11-05 19:56:01
【问题描述】:

如何安全地返回与列表中的项目匹配的字典项目?

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"
}

【问题讨论】:

  • 可惜字典是这样的

标签: c# list dictionary match equals


【解决方案1】:

因为字典的排列没有帮助,我想我可能会这样做:

var h = list.ToHashSet();
var result = dict.Where(kvp => h.Contains(kvp.Value));

【讨论】:

    猜你喜欢
    • 2019-07-15
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 2019-02-24
    • 2010-12-01
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    相关资源
    最近更新 更多