【发布时间】:2012-01-29 11:54:33
【问题描述】:
我有一个 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,TResult>)' 不能从用法中推断出来。尝试指定类型参数 明确的。
我关注了来自MSDN using the GroupJoin 的样本。我不想使用 2 个列表 - 我更喜欢使用包含所有行的 List<XElement> 的单个列表。
【问题讨论】:
标签: c# linq-to-xml