gpt4 book ai didi

c# - 从 C# List 中获取重复项
转载 作者:行者123 更新时间:2023-11-30 14:57:02 26 4
gpt4 key购买 nike

我有以下列表定义:

class ListItem
{
public int accountNumber { get; set; }
public Guid locationGuid { get; set; }
public DateTime createdon { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<ListItem> entitiesList = new List<ListItem>();
// Some code to fill the entitiesList
}
}

entitiesList 的accountNumbers 中有重复项。我想找到重复的 accountNumbers,对 locationGuids 执行一个操作,创建日期不是最近的重复创建日期。我如何操作列表以仅获取重复的 accountNumber、最近创建的 locationGuid 和(较旧的)locationGuid?

最佳答案

List<ListItem> entitiesList = new List<ListItem>();
//some code to fill the list
var duplicates = entitiesList.OrderByDescending(e => e.createdon)
.GroupBy(e => e.accountNumber)
.Where(e => e.Count() > 1)
.Select(g => new
{
MostRecent = g.FirstOrDefault(),
Others = g.Skip(1).ToList()
});

foreach (var item in duplicates)
{
ListItem mostRecent = item.MostRecent;
List<ListItem> others = item.Others;
//do stuff with others
}

关于c# - 从 C# List<object> 中获取重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21410153/

26 4 0