c# - 比较 2 个字典并返回缺失值
问题描述
我将如何比较这两个字典并仅返回缺少的值?
GetFileListFromBlob ()函数获取所有文件名,我想知道数据库中缺少什么。
还是有更好的方法从这些对象中获取缺失值?我应该使用不同的键/值吗?
Dictionary<int, string> databaseFileList = new Dictionary<int, string>;
Dictionary<int, string> blobFileList = new Dictionary<int, string>;
int counter = 0;
foreach (string f in GetFileListFromDB())
{
counter++;
databaseFileList.Add(counter, f );
}
counter = 0;
foreach (string f in GetFileListFromBlob())
{
counter++;
blobFileList.Add(counter, f);
}
// How to compare?
谢谢
解决方案
AHashSet<T>
可能是您想要的(而不是 a Dictionary<K,V>
) - 举个例子:
var reference = new HashSet<string> {"a", "b", "c", "d"};
var comparison = new HashSet<string> {"a", "d", "e"};
当您现在调用ExceptWith
参考集时...
reference.ExceptWith(comparison);
...reference
集合将包含集合"b"
中"c"
不存在的元素comparison
。但是请注意,"e"
不会捕获额外的元素(交换集合以获取"e"
缺失的元素)并且该操作会就地修改参考集。如果不希望这样做,则Except
LINQ 运算符可能值得研究,正如另一个答案中已经提到的那样。
推荐阅读
- c# - 使用 ASP.Net Core 进行数据注释本地化
- html - 直接在视图中从 Laravel 的元属性中删除 HTML 标签
- javascript - 自定义 Babel 插件 - 将 stringLiteral 值更改为实际的 Javascript 代码
- javascript - scrollIntoView 没有滚动到请求带有提交按钮 onClick
- nginx - 在 Engintron 问题上反向代理到端口 8069,而它适用于标准 NGINX 设置
- node.js - 通过Nodejs中的查询字符串获取用户
- matlab - 如何在matlab向量上制作多个if?
- c# - 从 .NET Core 2.2 迁移到 3.1 时缺少 .Relational() 方法
- javascript - 如何将来自外部源的时间作为 X 轴添加到 ChartJS 图表?
- styled-components - Ract 样式组件慢