2

我有一个对象和两个列表,如下所示:

    public class MyObject
    {
        public int Key;
        public DateTime Day;
        public decimal Value;
    }        

    List<MyObject> listA = new List<MyObject>()
    {
        new MyObject() { Key = 1, Day = new DateTime(2012, 12, 17), Value = 8 },
        new MyObject() { Key = 1, Day = new DateTime(2012, 12, 18), Value = 8 },
        new MyObject() { Key = 2, Day = new DateTime(2012, 12, 17), Value = 8 },
        new MyObject() { Key = 3, Day = new DateTime(2012, 12, 17), Value = 4 },
        new MyObject() { Key = 4, Day = new DateTime(2012, 12, 17), Value = 4 }
    };

    List<MyObject> listB = new List<MyObject>()
    {
        new MyObject() { Key = 1, Day = new DateTime(2012, 12, 17), Value = 2 },
        new MyObject() { Key = 1, Day = new DateTime(2012, 12, 18), Value = 8 },
        new MyObject() { Key = 3, Day = new DateTime(2012, 12, 17), Value = 8 },
        new MyObject() { Key = 4, Day = new DateTime(2012, 12, 17), Value = 4 },
        new MyObject() { Key = 5, Day = new DateTime(2012, 12, 17), Value = 10 }
    };

我正在寻找的结果是:

    List<MyObject> listChanges = new List<MyObject>()
    {
        new MyObject() { Key = 1, Day = new DateTime(2012, 12, 17), Value = -6 },
        new MyObject() { Key = 2, Day = new DateTime(2012, 12, 17), Value = -8 },
        new MyObject() { Key = 3, Day = new DateTime(2012, 12, 17), Value = 4 },
        new MyObject() { Key = 5, Day = new DateTime(2012, 12, 17), Value = 10 }
    };

本质上,我正在尝试创建一个列表,其中包含将 listA 变为 listB 所需的更改。因此,虽然可以使用 LINQ 中的 except 和 Intersect,但我认为它们不会有最好的性能来完成这样的任务,因为您仍然需要另一个比较来获得值的差异。

我的一个想法是:如果我遍历 listA,我可以从 listA 和 listB 中删除该项目(如果找到它,此时我可以确定 +/- 差异)。一旦我完成了 listA,listB 将只包含添加。

如何获得更改结果?

4

3 回答 3

0

首先,我将实现一个IEqualityComparer<T>基于KeyDay属性检查相等性的方法。然后你可以使用 linq 如下:

var notInA = listB.Except(listA, myEqualityComparer);
var notInB = listA.Except(listB, myEqualityComparer)
                   .Select(o => {
                      return new MyObject {
                         Key = item.Key,
                         Day = item.Day,
                         Value = item.Value * -1
                      };
                   });
var listA2 = listA.Intersect(listB, myEqualityComparer)
                  .OrderBy(o => o.Key)
                  .ThenBy(o => o.Day);
var listB2 = listB.Intersect(listA, myEqualityComparer)
                  .OrderBy(o => o.Key)
                  .ThenBy(o => o.Day);
var diff = listA2.Zip(listB2, (first,second) => {
   return new MyObject {
     Key = first.Key,
     Day = first.Day,
     Value = second.Value - first.Value
});
diff = diff.Concat(notInA).Concat(notInB);
于 2013-01-02T16:54:55.013 回答
0

这应该这样做。Key如果您的任何/Day组合在您的任何一个输入中都不是唯一的,它将引发异常。

public static IEnumerable<MyObject> GetChanges(
    IEnumerable<MyObject> from, IEnumerable<MyObject> to)
{
    var dict = to.ToDictionary(mo => new { mo.Key, mo.Day });

    // Check that keys are distinct in from, too:
    var throwaway = from.ToDictionary(mo => new { mo.Key, mo.Day });

    // Adjustments of items found in "from"
    foreach (MyObject mo in from)
    {
        var key = new { mo.Key, mo.Day };
        MyObject newVal;
        if (dict.TryGetValue(key, out newVal))
        {
            // Return item indicating adjustment
            yield return new MyObject { 
                Key = mo.Key, Day = mo.Day, Value = newVal.Value - mo.Value };
            dict.Remove(key);
        }
        else
        {
            // Return item indicating removal
            yield return new MyObject {
                Key = mo.Key, Day = mo.Day, Value = -mo.Value };
        }
    }

    // Creation of new items found in "to"
    foreach (MyObject mo in dict.Values)
    {
        // Return item indicating addition
        // (Clone as all our other yields are new objects)
        yield return new MyObject {
            Key = mo.Key, Day = mo.Day, Value = mo.Value };
    }
}

可以from通过删除唯一性检查或即时执行(尝试将每个项目的关键部分添加到 a )来加快这一速度HashSet,但我认为您不能避免循环to两次的一部分 - 一次构建字典,一次返回余数。

于 2013-01-02T16:55:21.503 回答
0

这个怎么样 :

        List<MyObject> listA = new List<MyObject>(){
            new MyObject() { Key = 1, Day = new DateTime(2012, 12, 17), Value = 8 },
            new MyObject() { Key = 2, Day = new DateTime(2012, 12, 17), Value = 8 },
            new MyObject() { Key = 1, Day = new DateTime(2012, 12, 18), Value = 8 },
            new MyObject() { Key = 4, Day = new DateTime(2012, 12, 17), Value = 4 },
            new MyObject() { Key = 3, Day = new DateTime(2012, 12, 17), Value = 4 }
        };

        List<MyObject> listB = new List<MyObject>(){
            new MyObject() { Key = 1, Day = new DateTime(2012, 12, 17), Value = 2 },
            new MyObject() { Key = 3, Day = new DateTime(2012, 12, 17), Value = 8 },
            new MyObject() { Key = 4, Day = new DateTime(2012, 12, 17), Value = 4 },
            new MyObject() { Key = 1, Day = new DateTime(2012, 12, 18), Value = 8 },
            new MyObject() { Key = 5, Day = new DateTime(2012, 12, 17), Value = 10 }
        };
        List<MyObject> listChanges = Comparer(listA, listB);
        MyObject[] hasil = listChanges.ToArray();
        for (int a = 0; a < hasil.Length;a++ ) {
            Console.WriteLine(hasil[a].Key+" "+hasil[a].Day+" "+hasil[a].Value);
        }

和功能:

    private MyObject[] sort(List<MyObject> input) {
        //sort input with it's key
        MyObject[] gg = input.ToArray();
        for (int a = 0; a < input.Count; a++) {
            for (int b = a + 1; b < input.Count; b++) {
                if (gg[a].Key > gg[b].Key) {
                    MyObject temp = gg[a];
                    gg[a] = gg[b];
                    gg[b] = temp;
                }
            }
        }
        //sort input, if key is same => sort the date
        for (int a = 0; a < input.Count; a++) {
            int indStart = a;
            int indEnd = a;
            for (int b = a + 1; b < input.Count; b++) {
                if (gg[a].Key == gg[b].Key) {
                    indEnd++;
                } else {
                    b = input.Count;
                }
            }
            a = indEnd;
            for (int c = indStart; c <= indEnd; c++) {
                for (int d = c + 1; d <= indEnd; d++) {
                    if (gg[c].Day > gg[d].Day) {
                        MyObject temp = gg[c];
                        gg[c] = gg[d];
                        gg[d] = temp;
                    }
                }
            }
        }
        return gg;
    }
    private List<MyObject> Comparer(List<MyObject> listA, List<MyObject> listB) {
        List<MyObject> output = new List<MyObject>();
        //if you sure that the list was sorted, u just remove the sort function
        MyObject[] ff = sort(listA);
        MyObject[] gg = sort(listB);
        Boolean[] masuk = new Boolean[gg.Length];
        //foreach element in listA, search the changes in input
        for (int a = 0; a < listA.Count;a++ ) {
            //find element in input which contains the changes of element in listA
            Boolean ins = false;
            for (int b = 0; b < listB.Count;b++ ) {
                if (masuk[b])
                    continue;
                if (ff[a].Key >= gg[b].Key) {
                    if (ff[a].Key == gg[b].Key && ff[a].Day == gg[b].Day){
                        masuk[b] = true;
                        if (gg[b].Value != ff[a].Value) {
                            output.Add(new MyObject() { Key = gg[b].Key, Day = gg[b].Day, Value = gg[b].Value - ff[a].Value });
                            b = listB.Count;
                        }
                        ins = true;
                    }
                } else {
                    b = listB.Count;
                }
            }
            if (!ins) {
                output.Add(new MyObject() { Key = ff[a].Key, Day = ff[a].Day, Value = -ff[a].Value });
            }
        }
        for (int a = 0; a < gg.Length;a++ ) {
            if(!masuk[a]){
                output.Add(new MyObject() { Key = gg[a].Key, Day = gg[a].Day, Value = gg[a].Value });
            }
        }
        return output;
    }

和输出:

    1 12/17/2012 12:00:00 AM -6
    2 12/17/2012 12:00:00 AM -8
    3 12/17/2012 12:00:00 AM 4
    5 12/17/2012 12:00:00 AM 10
于 2013-01-02T16:38:40.267 回答