gpt4 book ai didi

c# - Linq to XML - 字典转换

转载 作者:太空狗 更新时间:2023-10-29 22:56:10 25 4
gpt4 key购买 nike

如何使用 LINQ 将以下节点存储到字典中,其中 int 是自动生成的键和字符串(节点的值)?

Elements:

XElement instructors =
XElement.Parse(
@"<instructors>
<instructor>Daniel</instructor>
<instructor>Joel</instructor>
<instructor>Eric</instructor>
<instructor>Scott</instructor>
<instructor>Joehan</instructor>
</instructors>"
);

partially attempted code is given below :

var  qry = from instr in instructors.Elements("instructor")
where((p,index)=> **incomplete**).select..**incomplete**;

如何将我的选择变成Dictionary<int,String> ? (Key 应该从 1 开始;在 Linq 中索引从零开始)。

最佳答案

怎么样:

var dictionary = instructors.Elements("instructor")
.Select((element, index) => new { element, index })
.ToDictionary(x => x.index + 1,
x => x.element.Value);

关于c# - Linq to XML - 字典转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1593235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com