c# - 按基本路径过滤路径列表的最有效/优雅的方法是什么?

标签 c# .net algorithm path filtering

按基本路径过滤所有路径的最有效/最优雅的方法是什么?

我有一个路径列表和一个基本路径,我想要一个获取基本路径的子路径列表:

public IList<string> FilterPathList(IList<string> paths, string basePath)
{
  // return a list of paths that are children of the base path
}

示例输入:

c:\foo\bar\file1
c:\foo\bar\file2
c:\foo\bar\dir1\file11
c:\foo\bar\dir2\file
c:\foo\other\file1

Base path -> c:\foo\bar

预期输出:

c:\foo\bar\file1
c:\foo\bar\file2
c:\foo\bar\dir1\file11
c:\foo\bar\dir2\file

最佳答案

类似于:

paths.Where(p => p.StartsWith(basePath)).ToList();

您可能想要充实 Where 以使比较不区分大小写,当然,除非您将大小写标准化。

如果它在列表中,这也将返回基本路径。

关于c# - 按基本路径过滤路径列表的最有效/优雅的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6676794/

相关文章:

c# - 当加载播放器数据时,任何超过 400 的数字都会被减少到恰好 319

.net - 为什么 String.Empty 不是常量?

c# - 如何使用 OpenSSL.net 创建证书

c# - 有什么方法可以使 yield created Iterator 在出现异常时继续到下一个项目?

.net - 从 Linq To Sql 中的匿名类型查询获取单个结果

algorithm - 具有 O(1) 索引和 O(1) prev 和 next 的稀疏数组

node.js - 算法枚举序列

javascript - 如果我不知道集合的大小,我可以从集合中随机选择一个元素吗?

c# - 获取当前项目中文件的文件路径

C# : The best solution to parse the xml file?