【发布时间】:2011-05-15 10:44:12
【问题描述】:
我需要比较两个列表,每个列表包含大约 60,000 个对象。这样做最有效的方法是什么?我想选择源列表中不存在于目标列表中的所有项目。
我正在创建一个同步应用程序,其中 c# 扫描一个目录并将每个文件的属性放在一个列表中。因此有一个源目录列表和另一个目标目录列表。然后我不会复制所有文件,而是比较列表并查看哪些不同。如果两个列表都有相同的文件,那么我不会复制该文件。这是我使用的 Linq 查询,它在我扫描小文件夹时有效,但在扫描大文件夹时无效。
// s.linst is the list of the source files
// d.list is the list of the files contained in the destination folder
var q = from a in s.lstFiles
from b in d.lstFiles
where
a.compareName == b.compareName &&
a.size == b.size &&
a.dateCreated == b.dateCreated
select a;
// create a list to hold the items that are the same later select the outer join
List<Classes.MyPathInfo.MyFile> tempList = new List<Classes.MyPathInfo.MyFile>();
foreach (Classes.MyPathInfo.MyFile file in q)
{
tempList.Add(file);
}
我不知道为什么这个查询需要永远。还有其他我可以利用的东西。例如,我知道如果源文件与目标文件匹配,则不可能与该文件有另一个副本,因为不可能有相同名称和相同路径的文件名。
【问题讨论】:
标签: c# linq performance