我有两个包含字符串的列表,每个列表都存储路径信息。 List1
包含每个文件的完整 UNC 路径。 List2
包含每个路径的缩短版本。
我正在尝试使用来自 List2
的部分路径信息构建字典作为键,并将来自 List1
的完整路径信息作为值。
例如:
List1 = { "\\\\some\path111\to\file1.txt", "\\\\some\path222\to\file2.txt", "\\\\some\path333\to\file3.txt" };
List2 = { "\to\file3.txt", "\to\file2.txt", "\to\file1.txt" };
比较的预期结果:
{\to\file1.txt, \\\\some\path111\to\file1.txt}
{\to\file2.txt, \\\\some\path111\to\file2.txt}
{\to\file3.txt, \\\\some\path111\to\file3.txt}
我成功地编写了我需要的功能,但运行速度非常慢(见下文)。我想知道是否有任何方法可以加快速度或在不同的集合中存储信息以获得更快的匹配。这两个列表各包含大约500,000个字符串。
private Dictionary<string, string> FullPathBuilder(List<string> partialPathList, List<string> fullPathList)
{
Dictionary<string, string> result = new Dictionary<string, string>();
try
{
foreach (string partPath in partialPathList)
{
foreach (string matchedFullPath in fullPathList.Where(s => s.Contains(partPath)))
{
if (ThirdPartyRepackVariables.cancelQC)
{
break;
}
// get the match.
if (matchedFullPath != null)
{
if (!result.ContainsKey(partPath))
{
result.Add(partPath, matchedFullPath);
ThirdPartyRepackVariables.pathsUpdated++;
}
}
else
{
result.Add(partPath, partPath);
ThirdPartyRepackVariables.unmatchedPaths.Add(partPath);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error building path cross reference");
}
return result;
}