c# - Create a object hierarchy from a list of folder locations -


i have list of locations strings;

loca/locb loca/locb/loch locc/locd/loce locc/locd/loce/lock locf/locg 

i've been trying create object uses same structure list of locations passed it;

e.g. like..

var myhobject=createheirarchicalobjectfromlist(mystringlistoflocations); 

i'm having problems looping through list without doing manually loads of loops. there easier way, maybe recursion?

i want end object this;

.loca     .locb          .loch .locc     .locd          .loce               .lock .locf      .locg 

that can use create visual hierarchy.

prob not best knocked in linqpad, reformat in sec..

    void main()     {         var strings = new string[]{"loca/locb","loca/locb/loch",                          "locc/locd/loce","locc/locd/loce/lock","locf/locg"};          var folders = folder.parse(strings);          folders.dump();     }       public class folder     {         public string name { get; set; }          public list<folder> folders { get; internal set; }          public folder()         {             folders = new list<folder>();         }         //presume each string folder1/folder2/folder3         public static ienumerable<folder> parse(ienumerable<string> locations)         {             var folders = new list<folder>();             foreach (string location in locations)             {                 string[] parts = location.split('/');                 folder current = null;                 foreach (string part in parts)                 {                     var usefolders = current != null ?                                 current.folders : folders;                     current = usefolders.singleordefault(f => f.name == part) ?? new folder() { name = part };                     if (!usefolders.contains(current)) { usefolders.add(current); }                 }             }             return folders;         }     } 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -