我有 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/