Custom query against List<string>
I have a List<string> that contains paths, such as:
\Root\Folder1
\Root\Folder1\Folder1a
\Root\Folder1\Folder1a\Folder1aa
\Root\Folder2
Given a given root, such as \Root, I would like to "query" this list to return only the first level values, such as:
\Root\Folder1
\Root\Folder2
And "\Root\Folder1\", would return:
\Root\Folder1\Folder1a
How could I construct a query to do this?
Re: Custom query against List<string>
This is a working Console app:
vb.net Code:
Module Module1
Sub Main()
Dim allPaths = New List(Of String) From {"\Root\Folder1",
"\Root\Folder1\Folder1a",
"\Root\Folder1\Folder1a\Folder1aa",
"\Root\Folder2"}
Console.Write("Please enter a root path: ")
Dim rootPath = Console.ReadLine()
Do Until String.IsNullOrWhiteSpace(rootPath)
Dim rootPathLength = rootPath.Split(New Char() {"\"c}, StringSplitOptions.RemoveEmptyEntries).Length
Dim matchingPaths = allPaths.Where(Function(s) s.StartsWith(rootPath, StringComparison.CurrentCultureIgnoreCase) AndAlso
s.Split(New Char() {"\"c}, StringSplitOptions.RemoveEmptyEntries).Length = rootPathLength + 1)
Console.WriteLine("Matching paths:")
For Each path In matchingPaths
Console.WriteLine(path)
Next
Console.Write("Please enter a root path: ")
rootPath = Console.ReadLine()
Loop
End Sub
End Module
Re: Custom query against List<string>
By the way, your signature says VS 2008 while your thread title says VS 2010.