平面结果集到树视图层次结构

平面结果集到树视图层次结构

本文介绍了平面结果集到树视图层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下结果集,该结果集是作为Sql Server中的查询结果生成的.如何使用(C#)或(Linq to SQL)将其转换为层次结构数据源,以便将结果集绑定到ASP.NET Treeview控件.

I have the following result set that was generated as the result of query in Sql Server. How can I convert this into Hierarchical datasource using (C#) or (Linq to SQL) so I can bind the resultset to ASP.NET Treeview control.

1	Parent1	  10	Child1	  501	GrandChild1	Text
1	Parent1	  20	Child2	  502	GrandChild2	Text
1	Parent1	  30	Child3	  503	GrandChild3	Text
1	Parent1	  40	Child4	  0	                Text
2	Parent2	  10	Child1	  505	GrandChild4	Text
2	Parent2	  10	Child1	  506	GrandChild5	Text
2	Parent2	  20	Child2	  507	GrandChild6	Text
2	Parent2	  30	Child3	  508	GrandChild7	Text
2	Parent2	  40	Child4	  0		        Text

推荐答案


protected void Page_Load(object sender, EventArgs e)
{
    DataTable dt1 = new DataTable("DataTable1");
    dt1.Columns.Add("C1", typeof(string), null);
    dt1.Columns.Add("C2", typeof(string), null);
    dt1.Columns.Add("C3", typeof(string), null);
    dt1.Columns.Add("C4", typeof(string), null);
    dt1.Columns.Add("C5", typeof(string), null);
    dt1.Columns.Add("C6", typeof(string), null);
    dt1.Columns.Add("C7", typeof(string), null);
    dt1.Rows.Add("1", "Parent1", "10", "Child1", "501", "GrandChild1", "Text");
    dt1.Rows.Add("1", "Parent1", "20", "Child2", "502", "GrandChild2", "Text");
    dt1.Rows.Add("1", "Parent1", "30", "Child3", "503", "GrandChild3", "Text");
    dt1.Rows.Add("1", "Parent1", "40", "Child4", "0", "", "Text");
    dt1.Rows.Add("2", "Parent2", "10", "Child1", "505", "GrandChild4", "Text");
    dt1.Rows.Add("2", "Parent2", "10", "Child1", "506", "GrandChild5", "Text");
    dt1.Rows.Add("2", "Parent2", "20", "Child2", "507", "GrandChild6", "Text");
    dt1.Rows.Add("2", "Parent2", "30", "Child3", "508", "GrandChild7", "Text");
    dt1.Rows.Add("2", "Parent2", "40", "Child4", "0", "", "Text");

    XElement root = new XElement("Root");
    var query = dt1.AsEnumerable().GroupBy(dr => dr.Field<string>("C1")).Select(par => {
        XElement parent = new XElement("Parent");
        parent.Add(new XAttribute("Id", par.Key));
        parent.Add(new XAttribute("Name", par.First().Field<string>("C2")));
        par.GroupBy(dr1 => dr1.Field<string>("C3")).Select(ch => {
            XElement child = new XElement("Child");
            child.Add(new XAttribute("Id", ch.Key));
            child.Add(new XAttribute("Name", ch.First().Field<string>("C4")));
            ch.Select(gch => {
                if (gch.Field<string>("C5") != "0") {
                    XElement gChild = new XElement("GrandChild");
                    gChild.Add(new XAttribute("Id", gch.Field<string>("C5")));
                    gChild.Add(new XAttribute("Name", gch.Field<string>("C6")));
                    gChild.Add(new XAttribute("Value", gch.Field<string>("C7")));
                    child.Add(gChild);
                }
                return gch;
            }).Count();
            parent.Add(child);
            return ch;
        }
    ).Count();
        root.Add(parent);
        return par;
    }).Count();
    XmlDataSource1.Data = root.ToString();
}


LINQ querydeferred execution model的,因此直到它是iterated 或调用了Count 之类的scalar method时,它才被执行.

因此,在上面的代码中,Count extension方法用于强制立即执行查询.

TreeViewASP.NET标记如下:


The LINQ query is of deferred execution model, so that it will not be executed until it is iterated or a scalar method like Count is called on it.

Hence, in the above code Count extension method is used to force immediate execution of the query.

The ASP.NET markup for the TreeView is as follows:

<form id="form1" runat="server">
<div>
    <asp:TreeView ID="TreeView1" runat="server" DataSourceID="XmlDataSource1">
        <DataBindings>
            <asp:TreeNodeBinding DataMember="Parent" TextField="Name" ValueField="Id" />
            <asp:TreeNodeBinding DataMember="Child" TextField="Name" ValueField="Id" />
            <asp:TreeNodeBinding DataMember="GrandChild" TextField="Name" ValueField="Id" />
        </DataBindings>
    </asp:TreeView>

    <asp:XmlDataSource ID="XmlDataSource1" runat="server"

         XPath="Root/Parent"></asp:XmlDataSource>
</div>
</form>


LINQ 查询生成的XML 数据如下


The XML data generated by the LINQ query is as follows

<Root>
  <Parent Id="1" Name="Parent1">
    <Child Id="10" Name="Child1">
      <GrandChild Id="501" Name="GrandChild1" Value="Text" />
    </Child>
    <Child Id="20" Name="Child2">
      <GrandChild Id="502" Name="GrandChild2" Value="Text" />
    </Child>
    <Child Id="30" Name="Child3">
      <GrandChild Id="503" Name="GrandChild3" Value="Text" />
    </Child>
    <Child Id="40" Name="Child4" />
  </Parent>
  <Parent Id="2" Name="Parent2">
    <Child Id="10" Name="Child1">
      <GrandChild Id="505" Name="GrandChild4" Value="Text" />
      <GrandChild Id="506" Name="GrandChild5" Value="Text" />
    </Child>
    <Child Id="20" Name="Child2">
      <GrandChild Id="507" Name="GrandChild6" Value="Text" />
    </Child>
    <Child Id="30" Name="Child3">
      <GrandChild Id="508" Name="GrandChild7" Value="Text" />
    </Child>
    <Child Id="40" Name="Child4" />
  </Parent>
</Root>



TreeView 如下:



And the TreeView is as follows:

Parent1
    Child1
        GrandChild1
    Child2
        GrandChild2
    Child3
        GrandChild3
    Child4
Parent2
    Child1
        GrandChild4
        GrandChild5
    Child2
        GrandChild6
    Child3
        GrandChild7
    Child4


这篇关于平面结果集到树视图层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 04:19