使用c#有效识别CSV文件中已更改的字段

Gli*_*kot 5 c# algorithm diff

事实证明这比我想象的要困难.基本上,系统每天都会将客户主列表的快照转储为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 };
Run Code Online (Sandbox Code Playgroud)

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

a)使用字典或Hashsets-虽然早期的测试并没有真正显示出改进

b)对操作进行分区 - 使用客户参考字段中的第一个字符,并仅与具有相同字符的字符匹配.这可能涉及创建许多单独的集合,但似乎非常不优雅.

c)远离类型化数据安排并使用数组进行操作.再次,利益不确定.

有什么想法吗?

谢谢!

Jim*_*hel 4

为了下面讨论的目的,我假设您有某种方法将 CSV 文件读入类中。我将调用该类MyRecord

将文件加载到单独的列表中,调用它们NewListOldList

List<MyRecord> NewList = LoadFile("newFilename");
List<MyRecord> OldList = LoadFile("oldFilename");
Run Code Online (Sandbox Code Playgroud)

也许有一种更优雅的方法可以使用 LINQ 来完成此操作,但其想法是直接合并。首先,您必须对两个列表进行排序。要么你的MyRecord类实现IComparable,要么你提供你自己的比较委托:

NewList.Sort(/* delegate here */);
OldList.Sort(/* delegate here */);
Run Code Online (Sandbox Code Playgroud)

MyRecord如果实现了,您可以跳过委托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
}
Run Code Online (Sandbox Code Playgroud)

只有 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 };
Run Code Online (Sandbox Code Playgroud)

在上面,我假设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 };
Run Code Online (Sandbox Code Playgroud)

假设该CompareItems方法将对两个项目进行深度比较,如果比较相等则返回 0,如果某些内容发生更改则返回非零。