【发布时间】:2013-04-16 06:36:43
【问题描述】:
我正在使用 2 个列表,
List<string> localfiles = new List<string>();
List<string> remotefiles = new List<string>();
我需要在远程文件中找到本地文件中不存在的所有新项目。使用 LINQ 更容易,但我不能使用 Linq,因为我的应用程序在 .net 2.0 中。
【问题讨论】:
我正在使用 2 个列表,
List<string> localfiles = new List<string>();
List<string> remotefiles = new List<string>();
我需要在远程文件中找到本地文件中不存在的所有新项目。使用 LINQ 更容易,但我不能使用 Linq,因为我的应用程序在 .net 2.0 中。
【问题讨论】:
Dictionary<string, bool> files = new Dictionary<string, bool>();
foreach (var file in localfiles)
if (!files.ContainsKey(file))
files.Add(file, false);
List<string> result = new List<string>();
foreach (var file in remotefiles)
if (!files.ContainsKey(file))
result.Add(file);
Dictionary 更多 efficient 用于查找,然后 List 如果您有超过 3 个项目:
【讨论】:
+1 只是为了解决他的问题比 OP 付出更多的努力。
var temp = new List<string>();
foreach(var item in remoteFiles){
if(localfiles.Contains(item) == false){
temp.Add(item);
}
}
//temp 现在包含 remoteFiles 中本地文件中不存在的所有项目
【讨论】: