Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 1st, 2012  |  syntax: None  |  size: 1.72 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. show in treeview style
  2. Sore | aye
  3. A    |   1
  4. A    |   2
  5. A    |   3
  6. B    |   1
  7. B    |   2
  8.        
  9. A
  10.    1
  11.    2
  12.    3
  13. B  
  14.    1
  15.    2
  16.        
  17. public class MyObject
  18. {
  19.     public string Sore { get; set; }
  20.     public int aye { get; set; }
  21. }
  22.        
  23. var ls=new List<MyObject>();
  24. ls.Add(new UserQuery.MyObject(){Sore="A",aye=1});
  25. ls.Add(new UserQuery.MyObject(){Sore="A",aye=2});
  26. ls.Add(new UserQuery.MyObject(){Sore="A",aye=3});
  27. ls.Add(new UserQuery.MyObject(){Sore="B",aye=1});
  28. ls.Add(new UserQuery.MyObject(){Sore="B",aye=2});
  29.        
  30. var result=ls.GroupBy (l =>l.Sore)
  31.       .Select (l =>new
  32.                     {
  33.                         Root= l.Key,
  34.                         Children=l.Select (x =>x.aye)
  35.                     }
  36.                 ).ToList();
  37.        
  38. foreach (var root in result)
  39.     {
  40.         //root.Root to the root node
  41.         foreach(var child in root.Children)
  42.         {
  43.             //Add the child to the root nodes children
  44.         }
  45.     }
  46.        
  47. var str = "Sore | ayernA    |   1 rnA    |   2rnA    |   3rnB    |   1rnB    |   2";
  48.  
  49. var relations = str.Split(new[] {Environment.NewLine},
  50.                           StringSplitOptions.RemoveEmptyEntries)
  51.                    .Skip(1).Select(l => l.Split('|').Select(
  52.                                    x => x.Trim()).ToArray()).ToArray();
  53.  
  54. var relationsDic = new SortedDictionary<string, SortedSet<string>>();
  55.  
  56. foreach (var relation in relations)
  57. {
  58.     if (relationsDic.ContainsKey(relation[0]))
  59.     {
  60.         relationsDic[relation[0]].Add(relation[1]);
  61.     }
  62.     else
  63.     {
  64.         relationsDic[relation[0]] = new SortedSet<string> {relation[1]};
  65.     }
  66. }
  67.  
  68. foreach (var kvp in relationsDic)
  69. {
  70.     Console.WriteLine(kvp.Key);
  71.  
  72.     foreach (var sub in kvp.Value)
  73.     {
  74.         Console.WriteLine("t" + sub);
  75.     }
  76. }