本文发表在 rolia.net 枫下论坛Imports System.Linq
Public Class Class4
Public Sub AAA()
Dim myDS As DataSet = createTestDS()
Dim Xe As IEnumerable(Of XElement) = _
From Item In ( _
From E In myDS.Tables(0) _
Where ( _
From D In myDS.Tables(0) Where E("Parent") = D("Child") Select D _
).Count = 0 _
Select E("Parent") Distinct) _
Select Recursion(Item, myDS.Tables(0))
Dim strHtml As String = ""
For Each item In Xe
strHtml += item.ToString
Next
MsgBox(strHtml)
End Sub
Private Function Recursion(ByVal Name As String, ByVal DT As DataTable) As XElement
Dim myParentElement As XElement = _
<ul>
<li>
<%= Name %>
<%= From E In DT Where E("Parent") = Name Select Recursion(E("Child"), DT) %>
</li>
</ul>
Return myParentElement
End Function
Private Function createTestDS() As DataSet
Dim myDS As New DataSet
Dim myTB As New DataTable
Dim myCol As DataColumn
myCol = New DataColumn("Parent")
myTB.Columns.Add(myCol)
myCol = New DataColumn("Child")
myTB.Columns.Add(myCol)
myDS.Tables.Add(myTB)
Dim myDR As DataRow
'king(jones)
myDR = myTB.NewRow
myDR(0) = "king"
myDR(1) = "jones"
myTB.Rows.Add(myDR)
'jones(scott)
myDR = myTB.NewRow
myDR(0) = "jones"
myDR(1) = "scott"
myTB.Rows.Add(myDR)
'scott(adams)
myDR = myTB.NewRow
myDR(0) = "scott"
myDR(1) = "adams"
myTB.Rows.Add(myDR)
'jones(ford)
myDR = myTB.NewRow
myDR(0) = "jones"
myDR(1) = "ford"
myTB.Rows.Add(myDR)
'ford(smith)
myDR = myTB.NewRow
myDR(0) = "ford"
myDR(1) = "smith"
myTB.Rows.Add(myDR)
'king(blake)
myDR = myTB.NewRow
myDR(0) = "king"
myDR(1) = "blake"
myTB.Rows.Add(myDR)
'blake(allen)
myDR = myTB.NewRow
myDR(0) = "blake"
myDR(1) = "allen"
myTB.Rows.Add(myDR)
'blake(ward)
myDR = myTB.NewRow
myDR(0) = "blake"
myDR(1) = "ward"
myTB.Rows.Add(myDR)
'king(clark)
myDR = myTB.NewRow
myDR(0) = "king"
myDR(1) = "clark"
myTB.Rows.Add(myDR)
'clark(miller)
myDR = myTB.NewRow
myDR(0) = "clark"
myDR(1) = "miller"
myTB.Rows.Add(myDR)
Return myDS
End Function
End Class更多精彩文章及讨论,请光临枫下论坛 rolia.net
Public Class Class4
Public Sub AAA()
Dim myDS As DataSet = createTestDS()
Dim Xe As IEnumerable(Of XElement) = _
From Item In ( _
From E In myDS.Tables(0) _
Where ( _
From D In myDS.Tables(0) Where E("Parent") = D("Child") Select D _
).Count = 0 _
Select E("Parent") Distinct) _
Select Recursion(Item, myDS.Tables(0))
Dim strHtml As String = ""
For Each item In Xe
strHtml += item.ToString
Next
MsgBox(strHtml)
End Sub
Private Function Recursion(ByVal Name As String, ByVal DT As DataTable) As XElement
Dim myParentElement As XElement = _
<ul>
<li>
<%= Name %>
<%= From E In DT Where E("Parent") = Name Select Recursion(E("Child"), DT) %>
</li>
</ul>
Return myParentElement
End Function
Private Function createTestDS() As DataSet
Dim myDS As New DataSet
Dim myTB As New DataTable
Dim myCol As DataColumn
myCol = New DataColumn("Parent")
myTB.Columns.Add(myCol)
myCol = New DataColumn("Child")
myTB.Columns.Add(myCol)
myDS.Tables.Add(myTB)
Dim myDR As DataRow
'king(jones)
myDR = myTB.NewRow
myDR(0) = "king"
myDR(1) = "jones"
myTB.Rows.Add(myDR)
'jones(scott)
myDR = myTB.NewRow
myDR(0) = "jones"
myDR(1) = "scott"
myTB.Rows.Add(myDR)
'scott(adams)
myDR = myTB.NewRow
myDR(0) = "scott"
myDR(1) = "adams"
myTB.Rows.Add(myDR)
'jones(ford)
myDR = myTB.NewRow
myDR(0) = "jones"
myDR(1) = "ford"
myTB.Rows.Add(myDR)
'ford(smith)
myDR = myTB.NewRow
myDR(0) = "ford"
myDR(1) = "smith"
myTB.Rows.Add(myDR)
'king(blake)
myDR = myTB.NewRow
myDR(0) = "king"
myDR(1) = "blake"
myTB.Rows.Add(myDR)
'blake(allen)
myDR = myTB.NewRow
myDR(0) = "blake"
myDR(1) = "allen"
myTB.Rows.Add(myDR)
'blake(ward)
myDR = myTB.NewRow
myDR(0) = "blake"
myDR(1) = "ward"
myTB.Rows.Add(myDR)
'king(clark)
myDR = myTB.NewRow
myDR(0) = "king"
myDR(1) = "clark"
myTB.Rows.Add(myDR)
'clark(miller)
myDR = myTB.NewRow
myDR(0) = "clark"
myDR(1) = "miller"
myTB.Rows.Add(myDR)
Return myDS
End Function
End Class更多精彩文章及讨论,请光临枫下论坛 rolia.net