c# - 查找两本词典之间的差异

标签 c#

我有 2 个字典,其中包含来自 SAP Business 1 中 2 个数据库表的员工信息。它们有员工 ID 和薪水,例如我已确保第 1 表和第 2 表的员工 ID 始终相同

Table 1 (OHEM)

empID    salary
1        40000
2        56000
3        77000
4        80000 <------increase  


Table 2 (Salary Fitment)

empID    salary
1        40000
2        56000
3        77000
4        50000

在上面的例子中,如果 4 号员工增加/减少(或 OHEM 中的任何其他员工工资变化),我想比较两个字典然后更新 表二对应的工资。

代码

// Get service instances
var employeeService = Program.Kernel.Get<IEmployeeService>();
var salaryFitmentService = Program.Kernel.Get<ISalaryFitmentService>(); 

var OHEMDictionary = employeeService.GetAllEmployees().OrderBy(es => es.empID)
                                 .ToDictionary(od => od.empID,
                                               od => od.salary);

var SalaryFitmentDictionary = salaryFitmentService.GetAllSalaryFitments().Where(x => x.U_PD_Code.Trim().ToString() == "SYS001").OrderBy(es => es.U_Employee_ID)
                                          .ToDictionary(od => od.U_Employee_ID,
                                                              od => od.U_PD_Amount);

我已经有更新码了。获取字典差异以便更新差异的最佳方法是什么?

最佳答案

是这样的吗?

var diff = SalaryFitmentDictionary.Where(kv=>OHEMDictionary[kv.Key]!=kv.Value)

编辑

您还可以追加以获得每个员工的差异

.Select(kv => new { ID = kv.Key, Amount = OHEMDictionary[kv.Key] - kv.Value })

关于c# - 查找两本词典之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9326291/

相关文章:

c# - 在 silverlight 问题中绑定(bind)到 List<object>

c# - 获取DataContract中所有数据成员的名称

c# - 将函数从 c# 2008 转换为 Vb6.0 错误

c# - 合并 Linq 查询

c# - 如何动态地将值存储在 ArrayList 中

c# - 如何在后面的代码中使文本框为空

c# - 正则表达式的命名空间不起作用

c# - 在 mac 中没有找到匹配命令 "dotnet-aspnet-codegenerator"asp.net core 2.1 项目的可执行文件

c# - 消除.net lambda 表达式

c# - 通过引用传递 : child of multiple interfaces