按基本路径过滤所有路径的最有效/最优雅的方法是什么?
我有一个路径列表和一个基本路径,我想要一个获取基本路径的子路径列表:
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/