FileInfo的>二列表℃之间的差异; [英] Difference between two List<FileInfo>
问题描述
我可以用一个花哨的LINQ查询返回列表< FileInfo的>
,通过传递它的方法(列表< FileInfo的> oldList,清单< FileInfo的> newList
),看到有什么区别有两个列表之间
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?
基本上,我想要得到的任何名单文件添加到newList,这是不是在oldList用。
Basically, I want to get a list of any files added to newList, that were not available in oldList.
推荐答案
给定一个的IEqualityComparer
为的FileInfo
如下图所示:
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());
要找到添加到 newList
列表项,使用下面的代码:
To find items added to newList
list, use following code:
var addedItems = newList.Except(oldList, new FileInfoEqualityComparer());
这篇关于FileInfo的>二列表℃之间的差异;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!