如何将以下节点存储到Dictionary中,其中int是使用LINQ的自动生成的Key和字符串(节点的值)?
Elements:
XElement instructors =
XElement.Parse(
@"<instructors>
<instructor>Daniel</instructor>
<instructor>Joel</instructor>
<instructor>Eric</instructor>
<instructor>Scott</instructor>
<instructor>Joehan</instructor>
</instructors>"
);
Run Code Online (Sandbox Code Playgroud)
partially attempted code is given below :
var qry = from instr in instructors.Elements("instructor")
where((p,index)=> **incomplete**).select..**incomplete**;
Run Code Online (Sandbox Code Playgroud)
如何将我的选择变成Dictionary<int,String>?(键应从1开始;在Linq标记从零开始).
怎么样:
var dictionary = instructors.Elements("instructor")
.Select((element, index) => new { element, index })
.ToDictionary(x => x.index + 1,
x => x.element.Value);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3220 次 |
| 最近记录: |