LINQ到XML转换-dictionary [英] Linq to XML -Dictionary conversion

查看:123
本文介绍了LINQ到XML转换-dictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何存储以下内容解释其中int是使用LINQ?

元素:

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**;



如何把我的选择到词典< INT,字符串> ? (主要应该从1开始;在LINQ indicies从零开始)

How to turn my selection into Dictionary<int,String> ? (Key should start from 1;In Linq indicies start from Zero).

推荐答案

如何:

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

这篇关于LINQ到XML转换-dictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆