IEnumerable 集合中元素的序号位置 (Linq to XMl )
如何在此 linq 查询中嵌入元素的序号作为其属性。
var AllSections = from s in xmlDoc.Descendants("section")
select new
{
id = s.Attribute("id").Value,
themeTitle = s.Element("themeTitle").Value,
themeText = s.Element("themeText").Value,
objects = (from a in AllObjects
join b in s.Descendants("object")
on a.Attribute("accessionNumber").Value equals
b.Attribute("accessionNumber").Value
//select a
select new
{
//index = insert ordinal id/index of element
ObjectTitle = a.Element("ObjectTitle").Value,
ObjectText = a.Element("textentry").Value,
}
)
};
How do I embed the ordinal number of element as its attribute in this linq query.
var AllSections = from s in xmlDoc.Descendants("section")
select new
{
id = s.Attribute("id").Value,
themeTitle = s.Element("themeTitle").Value,
themeText = s.Element("themeText").Value,
objects = (from a in AllObjects
join b in s.Descendants("object")
on a.Attribute("accessionNumber").Value equals
b.Attribute("accessionNumber").Value
//select a
select new
{
//index = insert ordinal id/index of element
ObjectTitle = a.Element("ObjectTitle").Value,
ObjectText = a.Element("textentry").Value,
}
)
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法轻松地使用查询表达式来完成此操作 - 至少会产生可怕的副作用。但是,您可以使用
Select
或Where
的点表示法轻松完成此操作。假设您有一个相当长的查询表达式,那么在开头嵌入一个额外的调用可能是最简单的 - 假设您确实想要原始表达式中“s”的索引:假设您想要 < 的索引
AllObjects
中的 code>a。You can't easily do it with a query expression - at least not without a horrible side effect. However, you can easily do it with dot notation for either
Select
orWhere
. Given that you've got quite a long query expression, it's probably easiest to embed an extra call to where at the start - assuming you do actually want the index of "s" in the original expression:That's assuming you want the index of
a
withinAllObjects
.@Jon Skeet 为您提供了适当的 Select 重载来使用,并且这是您的查询中的内容:
@Jon Skeet gave you the appropriate overload of Select to use, and here is it in your query: