我有,類似於一個片段一個XDocument:如何從平面XDocument加入組?
<Data> <Row Id="0" ParentId="-1"> <!-- stuff --> </Row> <Row Id="1" ParentId="0"> <!-- stuff --> </Row> <Row Id="2" ParentId="0"> <!-- stuff --> </Row> <Row Id="3" ParentId="-1"> <!-- stuff --> </Row> <Row Id="4" ParentId="3"> <!-- stuff --> </Row> </Data>
假設嵌套僅限於上面的例子。 我想創建一個數據結構 - IDictionary<Parent, List<Child>>
。我似乎無法得到任何正確連接。我有這一點是:
// get lists of data nodes
List<XElement> pRows = xData.Elements(XName.Get("Row"))
.Where(e => e.Attribute(XParentId).Value == "-1")
.Select(e => e)
.ToList();
List<XElement> cRows = xData.Elements(XName.Get("Row"))
.Where(e => e.Attribute(XParentId).Value != "-1")
.Select(e => e)
.ToList();
var dataSets = pRows.GroupJoin(cRows,
p => p,
c => c.Attribute(XParentId).Value,
(p, children) => new {
ParentID = p.Attribute(XId).Value,
Children = children.Select(c => c)
});
編譯器抱怨:
類型參數的方法 「System.Linq.Enumerable.GroupJoin(System.Collections.Generic.IEnumerable, System.Collections.Generic.IEnumerable, System.Func,System.Func, System.Func,TResult>)' 無法根據用法推斷。嘗試明確指定類型參數 。
我跟着MSDN using the GroupJoin的樣本。我不想使用2個列表 - 我寧願使用包含所有行的單個列表List<XElement>
。