C# 以树景风格展示

C# 以树景风格展示,c#,C#,我的意见 Sore | aye A | 1 A | 2 A | 3 B | 1 B | 2 输出:我想将顶部的表排序到下面的树视图中 A 1 2 3 B 1 2 var str=“Sore | aye\r\nA | 1\r\nA | 2\r\nA | 3\r\nB | 1\r\nB | 2”; var relations=str.Split(new[]{Environment.NewLine}, St

我的意见

Sore | aye
A    |   1 
A    |   2
A    |   3
B    |   1
B    |   2
输出:我想将顶部的表排序到下面的树视图中

A
   1 
   2 
   3
B  
   1
   2 
var str=“Sore | aye\r\nA | 1\r\nA | 2\r\nA | 3\r\nB | 1\r\nB | 2”;
var relations=str.Split(new[]{Environment.NewLine},
StringSplitOptions.RemoveEmptyEntries)
.Skip(1)。选择(l=>l.Split(“|”)。选择(
x=>x.Trim()).ToArray()).ToArray();
var relationsDic=新的SortedDictionary();
foreach(关系中的var关系)
{
if(关系dic.ContainsKey(关系[0]))
{
关系dic[relations[0]]。添加(关系[1]);
}
其他的
{
relationsDic[relations[0]]=新的排序数据集{relations[1]};
}
}
foreach(关系中的var kvp)
{
控制台写入线(kvp.Key);
foreach(kvp.值中的var sub)
{
控制台写入线(“\t”+sub);
}
}
给定此对象:

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});
然后循环列表:

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 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
        }
    }