c# – 为Treenode创建一个列表

前端之家收集整理的这篇文章主要介绍了c# – 为Treenode创建一个列表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我知道这一定非常简单但我无法让它工作……我试图通过比较不同行中的两列并将它们作为节点或叶子相应地为extjs树创建一个List.这是我的样本数据
ListA  ListB  labelName
 NY           Parent1
        NY    Leaf1
 HI           Parent2
 AK           Parent3

这是我的c#end …所以当我匹配纽约时,我应该将Parent1作为节点而Leaf1作为它的叶子而不是HI或AK ……但是这样做会把所有数据都抛给我…即使是叶.

sqlCommand cmd = con.CreateCommand();

            comd.CommandText = "SELECT * FROM myTable";
            con.Open();
            sqlDataReader reader = comd.ExecuteReader();
            while (reader.Read())
            {
                City MyData = new City();

                MyData.ListA = reader["ListA"].ToString().Trim();
                MyData.ListB = reader["ListB"].ToString().Trim();
                MyData.labelName = reader["labelName"].ToString().Trim();
                giveData.Add(MyData);
            }

            int count = 1;

            List<TreeNode> myNode = new List<TreeNode>();
            foreach (City MyData in giveData)
            {
                // 1st foreach
                    if (MyData.ListA != "")
                    {

                        TreeNode treeNode = new TreeNode();
                        treeNode.id = count++;
                        treeNode.name = MyData.labelName;
                        treeNode.leaf = false;

                        List<TreeNode> Level1 = new List<TreeNode>();
                        foreach (City labelName  in giveData)
                        {
                            if (labelName.ListA == labelName.ListB)
                            {// 2nd foreach
                                TreeNode node1 = new TreeNode();
                                node1.id = count++;
                                node1.name = labelName.labelName;
                                node1.leaf = true;

                                Level1.Add(node1);
                            }
                        }

                        treeNode.children = Level1;
                        myNode.Add(treeNode);
                }
            }
            return JsonConvert.SerializeObject(myNode);

我应该使用数组来存储每条记录并将它们进行比较……我没有想法……我相信有更好的方法可以实现这一目标…请帮助

解决方法

假设数据是您所述的方式,并且“父母”将在任何叶子之前出现,这是我用来创建树的单一通道方式:

[不相关的代码截图]

更新:LINQ使用Dictionary< string,List< TreeNode>>

我创建了一个新类TreeNode和一些用于测试的示例数据:

var MyData = new List<City>
                  {
                     new City {ListA = "AK",ListB = "",labelName = "Alaska"},new City {ListA = "HI",labelName = "Hawaii"},new City {ListA = "",ListB = "HI",labelName = "Hawaii Leaf 1"},labelName = "Hawaii Leaf 2"},new City {ListA = "NY",labelName = "New York"},ListB = "NY",labelName = "New York Leaf 1"},labelName = "New York Leaf 2"}
                  };

这是基本创建2个列表的新方法,1个用于父级,1个用于叶子.然后我循环遍历叶子以找到任何匹配的父母并将叶子添加到它:

var index = 0;
var parents = (from p in MyData
               where p.ListB == ""
               select p).ToDictionary(p => p.ListA,p => new TreeNode { id = index++,name = p.labelName,leaf = false });

var leaves = (from l in MyData
              where l.ListA == ""
              group l by l.ListB into stateGroup
              select stateGroup).ToDictionary(g => g.Key,g => g.ToList());

foreach (var leaf in leaves.Where(leaf => parents.ContainsKey(leaf.Key)))
{
    parents[leaf.Key].children =
        leaf.Value.Select(l => new TreeNode {id = index++,name = l.labelName,leaf = true}).ToList();
}

var myNode = parents.Select(p => p.Value).ToList();

return JsonConvert.SerializeObject(myNode);

我认为这应该比使用列表和List.Find()更有效

猜你在找的C#相关文章