C# 差异和合并2个文本文件

C# 差异和合并2个文本文件,c#,text,C#,Text,所以我有一个时间限制的问题(子程序应该尽可能快)来做这件事。在文件的任何位置,我都有2个网络文件,如下所示: Time: 23.369 1464953512 Bytes: 4193304 Time: 24.369 1464953502 Bytes: 4194304 Time: 24.369 1464953502 Bytes: 4194304 Time: 25.404 1464953527 Bytes: 4194304 Time: 23.369 1464953512 Bytes: 419330

所以我有一个时间限制的问题(子程序应该尽可能快)来做这件事。在文件的任何位置,我都有2个网络文件,如下所示:

Time: 23.369
1464953512
Bytes: 4193304
Time: 24.369
1464953502
Bytes: 4194304

Time: 24.369
1464953502
Bytes: 4194304
Time: 25.404
1464953527
Bytes: 4194304
Time: 23.369
1464953512
Bytes: 4193304
Time: 24.369
1464953502
Bytes: 4194304
Time: 25.404
1464953527
Bytes: 4194304
请注意,这两个文件是不同的,每个文件都包含信息的子集,我需要合并这两个文件以创建累积信息(删除重复项),如下所示:

Time: 23.369
1464953512
Bytes: 4193304
Time: 24.369
1464953502
Bytes: 4194304

Time: 24.369
1464953502
Bytes: 4194304
Time: 25.404
1464953527
Bytes: 4194304
Time: 23.369
1464953512
Bytes: 4193304
Time: 24.369
1464953502
Bytes: 4194304
Time: 25.404
1464953527
Bytes: 4194304
最快的方法是什么?(如果可能,请附上一些代码)

谢谢

p.S.我在看一些,但我认为对同样的人来说,这将是一种过度的杀伤力。有什么简单的.net/LINQ魔术可以实现吗?另外,副本是串行的,如图所示,不分散。


编辑:-ve投票者请留下评论,以便我可以改进或以其他方式更改问题,使其更适合。

如果这对任何人都有帮助,我使用它查找两个文本文件的并集,之前已将它们转换为字符串枚举:

var dinfo = new DirectoryInfo(@"C:\http");
var files = dinfo.GetFiles("*.txt");
IEnumerable<string> _eValA = null;
IEnumerable<string> _eValB = null;

_eValA = File.ReadLines(@"C:\http\http1.txt");
_eValB = File.ReadLines(@"C:\http2.txt");

IEnumerable<String> union = _eValA.Union(_eValB);

//TODO: create file if does not exist
File.WriteAllLines(@"C:\http\union.txt", union.Cast<String>()); 
var dinfo=newdirectoryinfo(@“C:\http”);
var files=dinfo.GetFiles(“*.txt”);
IEnumerable_eValA=null;
IEnumerable_eValB=null;
_eValA=File.ReadLines(@“C:\http\http1.txt”);
_eValB=File.ReadLines(@“C:\http2.txt”);
IEnumerable并集=_eValA.union(_eValB);
//TODO:如果不存在,则创建文件
writeAllines(@“C:\http\union.txt”,union.Cast());

您将需要读入这些文件,并使用两个属性创建自定义类的实例:时间和字节。在自定义类中,重写Equals和GetHashCode方法,并让它们使用Time属性。例如:

public override int GetHashCode() {
    return Time.GetHashCode();
}

public override bool Equals(obj other) {
    //skipping type check and null check for brevity
    return Time.Equals(other.Time);
}

然后只需将您的项目添加到
哈希集
HashSet
不允许重复,所以您可以使用。

这些数字
1464953512
是某种密钥吗?它属于下面还是上面的字节/时间?如果是这样,您可以将两个文件中的对象粉碎为一个集合,然后使用该集合编写最终文件。或者,LINQ的except方法就是您要查找的。@saarrrr否这些数字完全是来自rpc的随机时间戳。它们是无用的,可能是因为某些遗留/其他原因而存在。因此,重复项可以以任何顺序分散?请查看HashSet.UnionWith。@hatchet no,重复项是串行的,如图所示。