2012-02-17 71 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