簡體   English   中英

比較兩個文件中的項目以確定哪些元素發生了變化

[英]Comparing Items From Two Files To Determine Which Elements Changed

我正在比較來自兩個文件(舊文件和新文件)的數據。 除了新文件中舊文件中已更改的項目之外,這些文件是相似的。 我需要將更改的項添加到更改的列表中,並將未更改的項添加到未更改的列表中。 我通過從舊文件中的新文件中搜索每個項目並比較文件元素來做到這一點。 如果找到新項目,則將其添加到未更改列表中,否則將其添加到更改列表中。

我現在要確定的是新文件中的哪個項目已更改。 如果未找到該項目,我如何確定這一點? 此外,新文件中的多個元素可能與舊文件不同。

這是片段:

foreach (var newfileItem in NewFile)
{
    var checkitem = OldFile.FirstOrDefault(d => d.fileID == newfileItem.fileID && d.SysName == newfileItem.Sysname && d.SysCount == newfileItem.SysCount);
    if (checkitem == null) //item not found therefore something changed
    {
        //which item changed??
        changedlist.Add(checkitem);
    }
    else //item found, therefore unchanged
    {
        unchangedlist.Add(newfileItem);
    }
    checkitem = null;
}

即使您使用“文件”這個詞,在我看來您已經完成了閱讀文件內容的部分。 而且我相信NewFileOldFile是一些類實例的列表,它們表示文件一行的內容。

所以我將它命名為FileItem ,看起來像這樣。

public class FileItem
{
    public int FileID { get; set; }
    public int SysCount { get; set; }
    public string SysName { get; set; }
}

而且我認為您本質上想找出哪些是OldFile獨有的項目,哪些是NewFile獨有的項目(也許還有兩者的共同點)。

為此,我將首先編寫一個EqualityComparer以便我可以比較兩個列表中的項目。 從您的代碼看來,如果該類的三個屬性相等,則您認為兩個項目相等。 所以我遵循這個邏輯。

public class FileComparer : IEqualityComparer<FileItem>
{
    public bool Equals(FileItem x, FileItem y)
    {
        return (x.FileID == y.FileID && x.SysCount == y.SysCount && x.SysName == y.SysName);
    }

    public int GetHashCode(FileItem obj)
    {
        unchecked
        {
            int hash = 17;
            if (obj != null)
            {
                hash = hash * 23 + obj.FileID.GetHashCode();
                hash = hash * 23 + obj.SysCount.GetHashCode();
                hash = hash * 23 + obj.SysName.GetHashCode();
            }
            return 0;
        }
    }
}

現在就像使用LINQ方法Except()Intersect()來獲得你想要的一樣簡單。

static void Main(string[] args)
{
    List<FileItem> OldFile = new List<FileItem>
    {
        new FileItem() { FileID = 1, SysCount = 100, SysName = "One" },
        new FileItem() { FileID = 2, SysCount = 200, SysName = "Two" },
        new FileItem() { FileID = 3, SysCount = 300, SysName = "Three" },
        new FileItem() { FileID = 4, SysCount = 400, SysName = "Four" },
        new FileItem() { FileID = 5, SysCount = 500, SysName = "Five" }
    };

    List<FileItem> NewFile = new List<FileItem>
    {
        new FileItem() { FileID = 1, SysCount = 100, SysName = "One" },
        new FileItem() { FileID = 200, SysCount = 200, SysName = "Two" },
        new FileItem() { FileID = 3, SysCount = 300, SysName = "Three" },
        new FileItem() { FileID = 400, SysCount = 400, SysName = "Four" },
        new FileItem() { FileID = 5, SysCount = 500, SysName = "Five" }
    };

    var onlyInNew = NewFile.Except(OldFile, new FileComparer());
    var onlyInOld = OldFile.Except(NewFile, new FileComparer());
    var commonToBoth = OldFile.Intersect(NewFile, new FileComparer());

    Console.ReadLine();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM