如何将当前行与同一个表中的上一行进行比较

时间:2012-10-15 05:44:39

标签: c# mysql

如何在MVC3.0中使用MySQL C#将当前记录与同一表中的前一条记录进行比较。

这是我的表

Historytable:

id  | projid|     task |   name         | description     |        date  |       type
----|-------|   -----  | -------------- |------------     |       -------|    ---------
1   |   1   |    sys21 |  validation    | validating user |      1-5-12  |    created
2   |   1   |    sys21 |  bug tracking  | background bug  |     23-7-12  |    updated
    |       |          |                |     tracking    |              |
3   |   1   |    sys21 |   bug tracking |  bug reporting  |    30-8-12   |   updated
4   |   1   |    sys21 |   bugs         |  bug reporting  |   12-9-12    |  updated
----------------------------------------------------------------------------------

现在我想要的结果是比较更新类型的记录与前一条记录,以便将之前的记录显示为通过与之前的记录进行比较而获得的先前历史和记录,并仅显示更新的字段作为当前历史记录

现在取决于projid检索历史。

我的观点如下:

previous history             current history
----------------             ---------------
type:                        created
name:                        validation
description:                 validating user

--------------------------------------------------------------

type:           created                         updated
name            validation                      bug tracking
description:    validating user                 background bug tracking

--------------------------------------------------------------------
type:           updated                         updated
name:           bug tracking                    bug report    
description:    background bug tracking         bug reporting

----------------------------------------------------------------
type:            updated                        updated
name:            bug tracking                   -
Description:     background bug tracking        bug reporting

------------------------------------------------------------------------
type:            updated                        updated
name:            bug tracking                   bugs
Description:     bug reporting                  -

我期待上面的输出,任何一个PLZ帮助我摆脱这种情况, 任何一个sugesions之王将被接受......

三江源,

1 个答案:

答案 0 :(得分:0)

我不确定我是否正确理解你,但你可以用以下逻辑来解决这个问题:

  1. 获取代表商品历史记录的行,并按日期降序排列
  2. 从上面获取第一行作为最后一次更改
  3. 从上一次到最后一次更改获取第二行
  4. 比较数据
  5. 这是一种潜在的方法(使用Linq):

    var history = db.History.Where(item => item.ProjId == 1)
                            .OrderByDescending(item => item.Date);
    var lastChange = history.First();
    var previousChange = history.Skip(1).First();
    

    现在您需要将上述行发送到比较方法。如果要突出显示更改,可以迭代行的属性并比较相同属性的值,如下所示:

    private IEnumerable<Tuple<string, object, object>> GetChangesBetweenRows(History row1, History row2)
    {
        var result = new List<Tuple<string, object, object>>();
        var properties = lastChange.GetType().GetProperties(); // both rows are of the same type
        foreach(var propInfo in properties)
        {
            var obj1 = propInfo.GetValue(lastChange, null);
            var obj2 = propInfo.GetValue(previousChange, null);
            if(obj1 != obj2)
                result.Add(Tuple.Create(propInfo.Name, obj1, obj2));
        }
        return result;
    }
    

    修改 给定上述方法,您可以遍历历史行的集合,并获得集合中任意两行之间的差异:

    static void Main(string[] args)
    {
        var history = db.History.Where(item => item.ProjId == 1)
                                .OrderBy(item => item.Date)
                                .ToArray();
        for(int i=1; i<history.Length; i++)
        {
            var diff = GetChangesBetweenRows(history[i-1], history[i]);
            DisplayDifferences(diff);
        }
    }
    
    static void DisplayDifferences(IEnumerable<Tuple<string, object, object>> diff)
    {
        foreach(var tuple in diff)
        {
            Console.WriteLine("Property: {0}. Object1: {1}, Object2: {2}",tuple.Item1, tuple.Item2, tuple.Item3);
        }
    }