Results 1 to 3 of 3

Thread: Custom query against List<string>

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    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?

    Visual Studio 2010

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Custom query against List<string>

    This is a working Console app:
    vb.net Code:
    1. Module Module1
    2.  
    3.     Sub Main()
    4.         Dim allPaths = New List(Of String) From {"\Root\Folder1",
    5.                                                  "\Root\Folder1\Folder1a",
    6.                                                  "\Root\Folder1\Folder1a\Folder1aa",
    7.                                                  "\Root\Folder2"}
    8.  
    9.         Console.Write("Please enter a root path: ")
    10.  
    11.         Dim rootPath = Console.ReadLine()
    12.  
    13.         Do Until String.IsNullOrWhiteSpace(rootPath)
    14.             Dim rootPathLength = rootPath.Split(New Char() {"\"c}, StringSplitOptions.RemoveEmptyEntries).Length
    15.             Dim matchingPaths = allPaths.Where(Function(s) s.StartsWith(rootPath, StringComparison.CurrentCultureIgnoreCase) AndAlso
    16.                                                            s.Split(New Char() {"\"c}, StringSplitOptions.RemoveEmptyEntries).Length = rootPathLength + 1)
    17.  
    18.             Console.WriteLine("Matching paths:")
    19.  
    20.             For Each path In matchingPaths
    21.                 Console.WriteLine(path)
    22.             Next
    23.  
    24.             Console.Write("Please enter a root path: ")
    25.  
    26.             rootPath = Console.ReadLine()
    27.         Loop
    28.     End Sub
    29.  
    30. End Module
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Custom query against List<string>

    By the way, your signature says VS 2008 while your thread title says VS 2010.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width