查找2个词典之间的差异

时间:2012-02-17 10:08:06

标签: 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);

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

6 个答案:

答案 0 :(得分:3)

这样的东西?

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

修改

您还可以附加以获得每位员工的差异

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

答案 1 :(得分: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;
}

特别是如果您只想报告更改类型:

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

哪个给出了

one different
three removed
five same
seven added

答案 2 :(得分:1)

没有足够的信息来涵盖所有基础(例如:一个字典中的密钥是另一个字典中的密钥的一部分吗?或者两个字典中的密钥的数量和值完全相同?),但通常这是我们在说什么:

foreach(var pair in SalaryFitmentDictionary)
{
    if(OHEMDictionary[pair.Key] != pair.Value)
    {
        // This employee's salary has changed
        OHEMDictionary[pair.Key] = pair.Value;
    }
}

答案 3 :(得分: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());
}

输出:

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

答案 4 :(得分: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};

答案 5 :(得分: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);
}