简体   繁体   中英

Difference between two List<FileInfo>

Can I use a fancy LINQ query to return a List<FileInfo> , by passing it in a method ( List<FileInfo> oldList, List<FileInfo> newList ), and seeing what differences there are between the two lists?

Basically, I want to get a list of any files added to newList, that were not available in oldList.

Given an IEqualityComparer for FileInfo shown below:

public class FileInfoEqualityComparer : IEqualityComparer<FileInfo>
{
    public bool Equals(FileInfo x, FileInfo y)
    {
        return x.FullName.Equals(y.FullName);
    }

    public int GetHashCode(FileInfo obj)
    {
        return obj.FullName.GetHashCode();
    }
}

You can use following code to find the difference between two lists:

var allItems = newList.Union(oldList);
var commonItems = newList.Intersect(oldList);
var difference = allItems.Except(commonItems, new FileInfoEqualityComparer());

To find items added to newList list, use following code:

var addedItems = newList.Except(oldList, new FileInfoEqualityComparer());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM