gpt4 book ai didi

c# - 使用 C# 高效地识别 CSV 文件中更改的字段

转载 作者:太空狗 更新时间:2023-10-29 23:10:49 26 4
gpt4 key购买 nike

事实证明,这比我想象的要难。基本上,系统每天都会将客户主列表的快照转储到 CSV 中。它包含大约 120000 条记录和 60 个字段。大约 25mb。无论如何,我想报告在一个快照和另一个快照之间发生变化的值。它不是计划文件差异,因为它必须与包含客户唯一编号的最左边的列值相匹配。可以插入/删除行等。所有字段都是字符串,包括引用号。

我已经使用 LINQ 编写了一个解决方案,但它因数据集较大而失效。对于 10000 条记录,需要 17 秒。 120000,两个文件对比,用了将近2个小时。现在它使用优秀且免费的“filehelpers”http://www.filehelpers.com/加载数据,这只需要几秒钟,然后。但是检测哪些记录发生了变化是比较有问题的。以下是 2 小时的查询:

    var changednames = from f in fffiltered
from s in sffiltered
where f.CustomerRef == s.CustomerRef &&
f.Customer_Name != s.Customer_Name
select new { f, s };

您会推荐什么方法?我想立即将列表“修剪”为那些有某种变化的列表,然后将我更具体的比较应用于那个小子集。我的一些想法是:

a) 使用字典或哈希集——尽管早期测试并没有真正显示出改进

b) 划分操作 - 使用客户引用字段中的第一个字符,并仅与具有相同字符的字符匹配。这可能涉及创建许多单独的集合,而且看起来很不雅观。

c) 放弃类型化数据排列并使用数组来实现。同样, yield 不确定。

有什么想法吗?

谢谢!

最佳答案

出于下面讨论的目的,我假设您有某种方法可以将 CSV 文件读入类中。我会调用那个类(class)MyRecord .

将文件加载到单独的列表中,将它们命名为NewListOldList :

List<MyRecord> NewList = LoadFile("newFilename");
List<MyRecord> OldList = LoadFile("oldFilename");

使用 LINQ 执行此操作可能有更优雅的方法,但想法是直接合并。首先,您必须对这两个列表进行排序。您的 MyRecord类(class)工具IComparable ,或者您提供自己的比较代表:

NewList.Sort(/* delegate here */);
OldList.Sort(/* delegate here */);

如果MyRecord,您可以跳过委托(delegate)工具 IComparable .

现在是直接合并。

int ixNew = 0;
int ixOld = 0;
while (ixNew < NewList.Count && ixOld < OldList.Count)
{
// Again with the comparison delegate.
// I'll assume that MyRecord implements IComparable
int cmpRslt = OldList[ixOld].CompareTo(NewList[ixNew]);
if (cmpRslt == 0)
{
// records have the same customer id.
// compare for changes.
++ixNew;
++ixOld;
}
else if (cmpRslt < 0)
{
// this old record is not in the new file. It's been deleted.
++ixOld;
}
else
{
// this new record is not in the old file. It was added.
++ixNew;
}
}

// At this point, one of the lists might still have items.
while (ixNew < NewList.Count)
{
// NewList[ixNew] is an added record
++ixNew;
}

while (ixOld < OldList.Count)
{
// OldList[ixOld] is a deleted record
}

只有 120,000 条记录,执行起来应该非常快。如果合并花费的时间与从磁盘加载数据一样长,我会感到非常惊讶。

编辑:LINQ 解决方案

我在思考如何使用 LINQ 来实现这一点。我不能做与上面的合并完全相同的事情,但我可以在单独的集合中获取添加、删除和更改的项目。
为此,MyRecord必须实现IEquatable<MyRecord>并覆盖 GetHashCode .

var AddedItems = NewList.Except(OldList);
var RemovedItems = OldList.Except(NewList);

var OldListLookup = OldList.ToLookup(t => t.Id);
var ItemsInBothLists =
from newThing in NewList
let oldThing = OldListLookup[newThing.Id].FirstOrDefault()
where oldThing != null
select new { oldThing = oldThing, newThing = newThing };

在上面,我假设 MyRecord有一个 Id独特的属性。

如果您只想要更改的项目而不是两个列表中的所有项目:

var ChangedItems =
from newThing in NewList
let oldThing = OldListLookup[newThing.Id].FirstOrDefault()
where oldThing != null && CompareItems(oldThing, newThing) != 0
select new { oldThing = oldThing, newThing = newThing };

假设 CompareItems方法将对两项进行深入比较,如果比较相等则返回 0,如果发生变化则返回非零。

关于c# - 使用 C# 高效地识别 CSV 文件中更改的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5240218/

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