7

我知道这一定很容易,但我无法让它工作......我正在尝试通过比较来自不同行的两列并将它们相应地放置为节点或叶子来为 extjs 树创建一个列表。这是我的样本数据

ListA  ListB  labelName
 NY           Parent1
        NY    Leaf1
 HI           Parent2
 AK           Parent3

这是我的 c# 结束...所以当我匹配 NY 时,我应该将 Parent1 作为节点,Leaf1 作为它的叶子,而不是 HI 或 AK...但是这样做会将我的所有数据都作为 Parent.. 甚至叶子。

            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);

我是否应该使用数组来存储每条记录并比较它们...我没有想法...我相信有更好的方法来完成这个...请帮助

4

3 回答 3

3

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

[无关代码被剪断]

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

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

var MyData = new List<City>
                  {
                     new City {ListA = "AK", ListB = "", labelName = "Alaska"},
                     new City {ListA = "HI", ListB = "", labelName = "Hawaii"},
                     new City {ListA = "", ListB = "HI", labelName = "Hawaii Leaf 1"},
                     new City {ListA = "", ListB = "HI", labelName = "Hawaii Leaf 2"},
                     new City {ListA = "NY", ListB = "", labelName = "New York"},
                     new City {ListA = "", ListB = "NY", labelName = "New York Leaf 1"},
                     new City {ListA = "", ListB = "NY", 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()

于 2012-12-26T21:56:18.007 回答
2

你最好的选择可能是 Linq - 我创建了一个快速而肮脏的 VB.Net 解决方案,让你朝着正确的方向前进 - 主要部分是第二个 Linq 语句......

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim x As New List(Of City)

    x.Add(New City With {.ListA = "NY", .ListB = "", .Leaf = "Parent1"})
    x.Add(New City With {.ListA = "", .ListB = "NY", .Leaf = "Leaf1"})
    x.Add(New City With {.ListA = "HI", .ListB = "", .Leaf = "Parent2"})
    x.Add(New City With {.ListA = "AK", .ListB = "", .Leaf = "Parent3"})

    tv1.Nodes.AddRange((From y In x Where y.ListA <> "" Select New TreeNode With {
                                                                    .Name = y.ListA,
                                                                    .Text = y.Leaf}).ToArray)

    For Each nd As TreeNode In tv1.Nodes
        Dim Nm As String = nd.Name
        nd.Nodes.AddRange((From y In x Where y.ListB = Nm Select New TreeNode(y.Leaf)).ToArray)
    Next
End Sub

简单地说,您填充父节点的第一个 ste,然后您只需遍历所有节点并使用 ListB = 节点名称的任何节点填充它们。

希望这有助于您朝着正确的方向前进

于 2012-12-26T22:09:59.473 回答
1

...我正在尝试用 C# 编写此代码,但应该这样做:

        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;

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

                        treeNode.Nodes.Add(node1);
                    }
                }

                myNode.Add(treeNode);
            }
        }
        return JsonConvert.SerializeObject(myNode);

希望这至少能让你朝着正确的方向前进......

于 2012-12-27T13:56:25.073 回答