我有一个对象列表(在下面的代码示例中为exobject),每个对象都有SomeId、AnotherId、SomeOtherId和Timestamp。这个列表可能有重复条目,每个记录的时间戳不同。我想删除所有具有较旧时间戳的此对象的重复项,仅保留最新的那些。
示例对象:
我的需求清单应该是:
我有一个使用C#非常粗糙的实现来做这件事。
我想知道是否有更优雅、更好的方法来做这件事,或者是否有一个lambda可以用来完成这个任务。
示例对象:
SomeId AnotherId SomeOtherId Timestamp
1 2 1 10
1 2 1 20
1 3 2 30
2 3 4 40
1 3 2 50
我的需求清单应该是:
1,2,1,20 and 1,3,2,50 and 2,3,4,40.
我有一个使用C#非常粗糙的实现来做这件事。
for (int i = 0; i < exObject.Count - 1; i++)
{
for (int j = i + 1; j < exObject.Count - 1; j++)
{
if (exObject[i].SomeId == exObject[j].SomeId && exObject[i].AnotherId == exObject[j].AnotherId && exObject[i].SomeOtherId == exObject[j].SomeOtherId)
{
if (exObject[i].TimeStamp < exObject[j].TimeStamp)
exObject[i].TimeStamp = exObject[j].TimeStamp;
exObject.Remove(exObject[j]);
}
}
}
我想知道是否有更优雅、更好的方法来做这件事,或者是否有一个lambda可以用来完成这个任务。