如何每次使用MVC3.0中的MySQL C#将当前记录与同一表中的前一个记录进行比较。
这是我的桌子
历史表:
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 -
我期待以上的输出,任何一位朋友帮助我摆脱困境,任何的国王都将被接受.
谢谢你,
发布于 2012-10-15 01:58:29
我不确定我是否正确地理解了您,但是您可以用以下逻辑来处理这个问题:
下面是一种潜在的方法(使用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);
}
}
https://stackoverflow.com/questions/12889909
复制相似问题