0

我已经实现了 2 个普通旧样式文本文件的逐行比较:

string[] linesOld = File.ReadAllLines(Path.Combine(directory, oldFileName));
string[] linesNew = File.ReadAllLines(Path.Combine(directory, newFileName));

for (int i = 1; i < linesNew.Length; i++)
{
    if (linesNew[i] != linesOld[i])
    {
        Console.WriteLine("Line: {0}, Old: {1}, New: {2}", i, linesOld[i], linesNew[i]);
    }
}

如何以新的 LINQ 样式重写此代码?

可能是我应该使用

linesNew.Except(linesOld);

?

4

1 回答 1

8

您可以使用以下Zip方法:

int i = 1;
foreach (var lines in linesOld.Zip(linesNew, (oldLine, newLine) =>
 new { oldLine, newLine, lineNumber = i++ }).Where(x => x.oldLine != x.newLine))
    Console.WriteLine("Line: {0}, Old: {1}, New: {2}",
                      lines.lineNumber, lines.oldLine, lines.newLine);
于 2013-11-13T15:49:32.490 回答