LINQ to XML 组查询
如果我有像这样的 xml:
<List>
<Item>
<Type>Type1</Type>
</Item>
<Item>
<Type>Type1</Type>
</Item>
<Item>
<Type>Type2</Type>
</Item>
</List>
我基本上想以某种方式创建一个字典或列表来表示这一点。这是我到目前为止所拥有的:
var grouped = (from p in _xmlDoc.Descendants("Item")
group p by p.Element("Blah").Value into Items
select Items);
我将如何选择它以便创建一个 Dictionary
其中 key 是分组的名称和 ("Type1" 或 "Type2 ”)。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不需要随机访问这些项目,请使用
查找< /code>
,这就是它的用途:
否则,如果您需要随机访问,则将它们放入字典中:
If you don't need random access to the items, use a
Lookup
, that's what it's there for:Otherwise, if you need random access, then throw them into a dictionary:
类似这样,您必须检查正确的语法,但基本上当您按某些内容进行分组时,您也有键和值,并且您可以使用 ToDictionary 方法将其转换为字典。
Something like that, you'll have to check the correct syntax, but basically when you group by something, you have the Key and Value too, and you can use ToDictionary method to transform this into a dictionary.