【发布时间】:2013-12-09 10:58:06
【问题描述】:
我有以下 XML:
<R N="1">
<MT N="Section" V="Section-1" />
<MT N="Person" V="John" />
</R>
<R N="2">
<MT N="Section" V="Section-1" />
<MT N="Person" V="Peter" />
</R>
<R N="3">
<MT N="Section" V="Section-2" />
<MT N="Person" V="Joseph" />
</R>
... ...
<R N="N">
<MT N="Section" V="Section-J" />
<MT N="Person" V="PersonX" />
</R>
以及以下生成IEnumerable<IGrouping<string,XElement>> 的GroupBy 子句:
var something = MyElements.GroupBy
(
x => x.Elements("MT")
.First
(
type => type.Attribute("N").Value == "Section"
)
.Attribute("V").Value
,
x=>x
);
我希望每组最多包含 4 个项目。
我不能对表达式x=>x 使用Take 方法,因为它不是IEnumerable<T>。
你知道如何限制每个组的结果吗?
【问题讨论】:
-
你能描述一下你想从xml中选择什么数据吗?看起来您的查询不是您可以使用的最佳查询
标签: c# .net linq lambda linq-to-xml