简体   繁体   中英

how to compare current row with the previous row in the same table

How to compare everytime, the current record with the previous record in the same table using MySQL C# in MVC3.0.

This is my table

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

now i want the result such that compare the record of type updated with the previous record in order to show the previous record as the previous history and record obtained by comparing with the previous record and display only the updated fields as the current history.

now depending upon the projid retrieve the history.

and my view look like the below:

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                  -

I am expecting the above output, any one plz help me out from the situation, any king of sugesions will be accepted...

Thankyou,

I am not sure I understood you correctly but you could approach this with the following logic:

  1. Get the rows that represent the history of an item and order by date descending
  2. Get the first row from above as the last change
  3. Get the second row from 1. as the previous to last change
  4. Compare the data

Here's a potential approach for this (using Linq):

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

Now you need to send the above rows to your comparison method. If you want to highlight the changes, you can iterate through properties of the rows and compare values for same properties like this:

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

EDIT Given the method above, you can iterate through a collection of history rows and get differences between any of two rows in the collection:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM