gpt4 book ai didi

c# - 将当前列表数据与旧列表数据进行比较

转载 作者:太空宇宙 更新时间:2023-11-03 12:05:10 24 4
gpt4 key购买 nike

我必须将当前列表 的数据与上一个 的数据列表进行比较。在我的列表类中,我有

Id, 
Comment,
Updatedby,
Updated Dt,
IsEdited

和其他几个领域。我正在比较如下。

foreach (var cscData in Currentcloses)
{
if (cscData.Status == ReferenceEnums.ActionStatus.EDIT)
{
if (Previoussoftcloses != null)
{
foreach (var pscData in Previouscloses)
{
if (cscData.Id == pscData.Id)
{
//my logic goes here
}
}
}
}
}

除此之外还有什么更好的方法。只是想检查一下。

新代码

 var currentData = Currentsoftcloses
.Where(c => c.Status == ReferenceEnums.ActionStatus.EDIT);

foreach (var cscData in currentData)
{
if (Previoussoftcloses != null)
{
var previous = Previoussoftcloses
.GroupBy(item => item.Id)
.ToDictionary(chunk => chunk.Key, chunk => chunk.First());

if (previous.TryGetValue(cscData.Id, out var pscData))
{
//my logic goes here
}
} }

最佳答案

您可以摆脱内部循环;如果 Previouscloseslong,您的代码将更快:O(|Previoussoftcloses| + |Currentcloses|)O(|Previoussoftcloses| * |Currentcloses|) 时间复杂度。

// If Previoussoftcloses == null we can do nothing, let's check for this possibility
if (Previoussoftcloses != null) {
// Dictionary is faster then List on Contains O(1) vs. O(N)
var previous = Previouscloses
.GroupBy(item => item.Id)
.ToDictionary(chunk => chunk.Key, chunk => chunk.First());

// Readability: what we are going to scan
var current = Currentcloses
.Where(c => c.Status == ReferenceEnums.ActionStatus.EDIT);

foreach (var cscData in current) {
if (previous.TryGetValue(cscData.Id, out var pscData)) {
//my logic goes here
}
}
}

关于c# - 将当前列表数据与旧列表数据进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55417758/

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