我有一个数据表(邻接表)有2列Parent,Child 我试图找出顶级节点(没有父节点的节点),如果我在SQL中这样做,它将类似于此
Select distinct Parent from theTable
Where Parent Not IN (Select Child from theTable)
parent/child
1/2
1/3
3/4
4/5
在这种情况下,顶部节点将是1
如何在VB.Net中使用带有数据表的LINQ来实现这一点?
像这样的非工作的suedo:
Dim root As DataRow = From p In theTable.Rows
Where p.Parent not in (from c in theTable.Rows select c.Child)
Select r.Parent
谢谢!
答案 0 :(得分:0)
在DataTable
上执行linq查询时,您应该使用AsEnumerable
来访问这些行。您的查询变为:
(From r In theTable.AsEnumerable
Where Not (
From c In theTable.AsEnumerable
Select c.Field(Of Integer)("Child")
).Contains(r.Field(Of Integer)("Parent"))
Select r.Field(Of Integer)("Parent")).Distinct