tags:

views:

420

answers:

1

How to store the nodes of the following into Dictionary where int is an autogenerated Key and string (value of the node) using 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**;

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

+2  A: 

How about:

var dictionary = instructors.Elements("instructor")
                            .Select((element, index) => new { element, index })
                            .ToDictionary(x => x.index + 1,
                                          x => x.element.Value);
Jon Skeet
Yes it is working very fine ;Thanks
Have you written any special articles on LINQ ?
Not really - I blog about it every so often though.
Jon Skeet