2012-02-17 58 views
1

我有2个包含员工信息的词典,它们包含SAP Business 1中的2个数据库表。他们有员工ID和薪水例如我已经确保了第一和第二表的雇员ID将始终是相同的查找2个词典之间的差异

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); 

我已经有一个更新的代码。获得字典差异的最佳方法是什么,以便我可以更新差异?

回答

3

是这样的?

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

编辑

您还可以追加得到的差异为每一位员工

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

,只是'AB

var first = new Dictionary<string, string>() { { "one", "two" }, { "three", "four" }, { "five", "six" } }; var second = new Dictionary<string, string>() { { "one", "2" }, { "five", "six" }, { "seven", "eight" } }; foreach (var entry in DiffDictionary(first, second)) { Console.WriteLine("{0} {1} ", entry.Key, entry.Value); } 

'不'(AB)U(BA)'对吗? – naveen 2012-02-17 10:19:30

+0

@naveen,你是对的,但是这个'OHEMDictionary = employeeService.GetAllEmployees'让我认为OHEMDictionary包含所有员工。 – 2012-02-17 10:23:47

+0

+1:是的,这是真的。我没有正确阅读。那会做。 – naveen 2012-02-17 10:32:58

1

没有足够的信息来涵盖所有的基础(例如:一个字典中的键是另一个字典中的一个严格子集吗?或者这两个字典的键和键的数量和值完全相同?),但通常这是我们正在谈论的:

foreach(var pair in SalaryFitmentDictionary) 
{ 
    if(OHEMDictionary[pair.Key] != pair.Value) 
    { 
     // This employee's salary has changed 
     OHEMDictionary[pair.Key] = pair.Value; 
    } 
} 
0
var result = from o in OHEMDictionary 
       join f in SalaryFitmentDictionary on o.Key equals f.Key 
       where o.Value != f.Value 
       select { o.Key, o.Value - f.Value}; 
0

这将只包括那些同时出现在字典中的项目,但你可以做这样的事情:

Dictionary<int, string> first = new Dictionary<int, string> 
     { { 1, "One" }, { 2, "Two" }, { 3, "Three" } }; 
Dictionary<int, string> second = new Dictionary<int, string> 
     { { 1, "One" }, { 2, "Two" }, { 3, "Tri" } }; 

var difference = from f in first 
       join s in second on f.Key equals s.Key 
       where f.Value != s.Value 
       select new {Key = f.Key, 
          FirstValue = f.Value, 
          SecondValue = s.Value }; 

foreach (var item in difference) 
{ 
    Console.WriteLine("Different item with key {0}, Values {1} and {2}", 
          item.Key, item.FirstValue, item.SecondValue); 
} 
1

创建一个类来包含的区别:

public class DictionaryDifference<TKey, TValue> 
{ 
    public TKey Key 
    { 
     get; 
     set; 
    } 

    public TValue OriginalValue 
    { 
     get; 
     set; 
    } 

    public TValue NewValue 
    { 
     get; 
     set; 
    } 
} 

创建一个扩展方法,找到差异:

public static class DictionaryExtensions 
{ 
    public static IEnumerable<DictionaryDifference<TKey, TValue>> GetDifferencesFrom<TKey, TValue>(
     this IDictionary<TKey, TValue> original, 
     IDictionary<TKey, TValue> latest) 
     where TValue : IComparable 
    { 
     foreach (var originalItem in original) 
     { 
      if (latest.ContainsKey(originalItem.Key)) 
      { 
       if (originalItem.Value.CompareTo(latest[originalItem.Key]) != 0) 
       { 
        // The key is in the latest but the value is different. 
        yield return new DictionaryDifference<TKey, TValue> 
        { 
         Key = originalItem.Key, 
         OriginalValue = originalItem.Value, 
         NewValue = latest[originalItem.Key] 
        }; 
       } 
      } 
      else 
      { 
       // The key is not in the latest dictionary. 
       yield return new DictionaryDifference<TKey, TValue> 
       { 
        Key = originalItem.Key, 
        OriginalValue = originalItem.Value, 
        NewValue = default(TValue) 
       }; 
      } 
     } 

     foreach (var newItem in latest) 
     { 
      if (!original.ContainsKey(newItem.Key)) 
      { 
       // The key is not in the original dictionary. 
       yield return new DictionaryDifference<TKey, TValue> 
       { 
        Key = newItem.Key, 
        OriginalValue = default(TValue), 
        NewValue = latest[newItem.Key] 
       }; 
      } 
     } 
    } 
} 

创建2个字典和比较:

var dictionary1 = new Dictionary<int, double>(); 
dictionary1.Add(1, 40000); 
dictionary1.Add(2, 56000); 
dictionary1.Add(3, 77000); 
dictionary1.Add(4, 80000); 
dictionary1.Add(5, 100000); 

var dictionary2 = new Dictionary<int, double>(); 
dictionary2.Add(1, 40000); 
dictionary2.Add(2, 56000); 
dictionary2.Add(3, 77000); 
dictionary2.Add(4, 50000); 
dictionary2.Add(6, 35000); 

foreach (var difference in dictionary1.GetDifferencesFrom(dictionary2)) 
{ 
    Console.WriteLine(
     "Key {0} was {1} but is now {2}", 
     difference.Key.ToString(), 
     difference.OriginalValue.ToString(), 
     difference.NewValue.ToString()); 
} 

OUTPUT:

Key 4 was 80000 but is now 50000 
Key 5 was 100000 but is now 0 
Key 6 was 0 but is now 35000 
+0

谢谢你的回答。 – 2012-02-17 11:00:15

+0

我很喜欢这个解决方案。我已经改变了我的实现中的foreach循环来处理空的旧/新差异 – 2014-01-15 19:43:51

2

这是一个简单的,但方便的功能:

private static Dictionary<string, string> DiffDictionary(Dictionary<string, string> first, Dictionary<string, string> second) 
{ 
    var diff = first.ToDictionary(e => e.Key, e => "removed"); 
    foreach (var other in second) 
    { 
     string firstValue; 
     if (first.TryGetValue(other.Key, out firstValue)) 
     { 
      diff[other.Key] = firstValue.Equals(other.Value) ? "same" : "different"; 
     } 
     else 
     { 
      diff[other.Key] = "added"; 
     } 
    } 
    return diff; 
} 

特别是如果你只是想报告变更的类型:其中给出

one different 
three removed 
five same 
seven added