
Untitled
By: a guest on
Aug 1st, 2012 | syntax:
None | size: 1.72 KB | hits: 10 | expires: Never
show in treeview style
Sore | aye
A | 1
A | 2
A | 3
B | 1
B | 2
A
1
2
3
B
1
2
public class MyObject
{
public string Sore { get; set; }
public int aye { get; set; }
}
var ls=new List<MyObject>();
ls.Add(new UserQuery.MyObject(){Sore="A",aye=1});
ls.Add(new UserQuery.MyObject(){Sore="A",aye=2});
ls.Add(new UserQuery.MyObject(){Sore="A",aye=3});
ls.Add(new UserQuery.MyObject(){Sore="B",aye=1});
ls.Add(new UserQuery.MyObject(){Sore="B",aye=2});
var result=ls.GroupBy (l =>l.Sore)
.Select (l =>new
{
Root= l.Key,
Children=l.Select (x =>x.aye)
}
).ToList();
foreach (var root in result)
{
//root.Root to the root node
foreach(var child in root.Children)
{
//Add the child to the root nodes children
}
}
var str = "Sore | ayernA | 1 rnA | 2rnA | 3rnB | 1rnB | 2";
var relations = str.Split(new[] {Environment.NewLine},
StringSplitOptions.RemoveEmptyEntries)
.Skip(1).Select(l => l.Split('|').Select(
x => x.Trim()).ToArray()).ToArray();
var relationsDic = new SortedDictionary<string, SortedSet<string>>();
foreach (var relation in relations)
{
if (relationsDic.ContainsKey(relation[0]))
{
relationsDic[relation[0]].Add(relation[1]);
}
else
{
relationsDic[relation[0]] = new SortedSet<string> {relation[1]};
}
}
foreach (var kvp in relationsDic)
{
Console.WriteLine(kvp.Key);
foreach (var sub in kvp.Value)
{
Console.WriteLine("t" + sub);
}
}