What is the most efficient/elegant way to filter all paths by a base path?
I have a path list and a base path, and I want a to get a list of paths that are children of the base path:
public IList<string> FilterPathList(IList<string> paths, string basePath)
{
// return a list of paths that are children of the base path
}
Sample input:
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
Expected output:
c:\foo\bar\file1
c:\foo\bar\file2
c:\foo\bar\dir1\file11
c:\foo\bar\dir2\file
Something like:
paths.Where(p => p.StartsWith(basePath)).ToList();You may want to flesh out the Where to make the comparison case-insensitive, unless of course, you normalise the case.
This will also return the base path if it’s in the list.