gpt4 book ai didi

c# - 根据 C# 中的条件从 List 中删除重复项

转载 作者:行者123 更新时间:2023-12-05 03:11:36 25 4
gpt4 key购买 nike

我有一个对象列表(下面代码示例中的 exobject),每个对象都有 SomeId、AnotherId、SomeOtherId 和 Timestamp。此列表可能有重复的条目,每条记录都有不同的时间戳。我想用旧的时间戳删除这个对象的所有重复项,只保留最新的。

示例对象:

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 可以用来完成此操作。

最佳答案

System.Linq 有一个 Distinct 方法。您必须实现 IEqualityComparer。详细如何在这里...

https://msdn.microsoft.com/en-us/library/bb338049(v=vs.110).aspx


根据您的评论进行编辑:如果您执行 orderBy,它应该保留您想要的...这里有一些代码...

using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var data = new[]
{
new SomeClass { SomeId = 1, AnotherId = 1, SomeOtherId = 1, Timestamp = 10 },
new SomeClass { SomeId = 1, AnotherId = 1, SomeOtherId = 1, Timestamp = 20 }, // Duplicate
new SomeClass { SomeId = 1, AnotherId = 2, SomeOtherId = 2, Timestamp = 30 },
new SomeClass { SomeId = 1, AnotherId = 2, SomeOtherId = 2, Timestamp = 35 }, // Duplicate
new SomeClass { SomeId = 2, AnotherId = 4, SomeOtherId = 4, Timestamp = 40 },
new SomeClass { SomeId = 3, AnotherId = 2, SomeOtherId = 2, Timestamp = 50 },
new SomeClass { SomeId = 1, AnotherId = 1, SomeOtherId = 1, Timestamp = 50 } // Duplicate
};

var distinctList = data
.OrderBy(x => x.Timestamp)
.Distinct(new SomeClassComparer())
.ToList();
}

public class SomeClass
{
public int SomeId { get; set; }
public int AnotherId { get; set; }
public int SomeOtherId { get; set; }
public int Timestamp { get; set; }
}

public class SomeClassComparer : IEqualityComparer<SomeClass>
{
public bool Equals(SomeClass x, SomeClass y)
{
if (ReferenceEquals(x, y))
{
return true;
}

//Check whether any of the compared objects is null.
if (ReferenceEquals(x, null) || ReferenceEquals(y, null))
{
return false;
}

//Check whether the SomeClass's properties are equal.
return x.SomeId == y.SomeId &&
x.AnotherId == y.AnotherId &&
x.SomeOtherId == y.SomeOtherId;
}

public int GetHashCode(SomeClass someClass)
{
//Check whether the object is null
if (ReferenceEquals(someClass, null))
{
return 0;
}

//Get hash code for the fields
var hashSomeId = someClass.SomeId.GetHashCode();
var hashAnotherId = someClass.AnotherId.GetHashCode();
var hashSomeOtherId = someClass.SomeOtherId.GetHashCode();

//Calculate the hash code for the SomeClass.
return (hashSomeId ^ hashAnotherId) ^ hashSomeOtherId;
}
}
}
}

关于c# - 根据 C# 中的条件从 List<T> 中删除重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36635118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com