c# - 联合词典

标签 c# linq dictionary

我有两个Dictionary<Person,Boolean> 。 字典 a 包含:

Person A -> false
Person B -> true,
Person C -> false;

字典 b 包含:

Person A -> true;
Person D -> false;

我想要一个结果,其中一次包含所有人员,如果一个人员在两个列表之一中都包含 true,则将 bool 值设置为 ture。

我该如何解决这个问题 dict.Union()

谢谢小基

最佳答案

听起来你可以使用:

var result = first.Union(second)
                  .GroupBy(x => x.Key)                // Group by dictionary keys
                  .ToDictionary(g => g.Key,           // Key for new dictionary
                                g => g.Any(p => p.Value)); // Any true values?

关于c# - 联合词典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12781542/

相关文章:

c# - Entity Framework : Introducing FOREIGN KEY constraint '' on table '' may cause cycles or multiple cascade paths

c# - 模板参数中的 "Covariance"

c# - OfType<T>() 是如何工作的?

c# - 在 ListArray C# 中搜索

Perl 映射/grep 内存泄漏

python - 从 .json 文件中获取特定数据并将它们保存到 python 中的 2D 矩阵/字典中

arrays - Swift 中的字典数组

c# - 使用 Ninject 自定义 OptionalAttribute

c# - 如何在不迭代 blob 列表的情况下获取 Azure Blob 容器的大小?

linq - 动态地将类型传递给方法<T>