views:

537

answers:

2

How do I embed the ordinal number of element as its attribute in this linq query.

var AllSections = from s in xmlDoc.Descendants("section")
                      select new
                      {
                          id = s.Attribute("id").Value,
                          themeTitle = s.Element("themeTitle").Value,
                          themeText = s.Element("themeText").Value,
                          objects = (from  a in AllObjects 
                                     join b in s.Descendants("object")
                                     on  a.Attribute("accessionNumber").Value equals
                                         b.Attribute("accessionNumber").Value
                                      //select a
                                      select new
                                       {
                                        //index = insert ordinal id/index of element

                                        ObjectTitle = a.Element("ObjectTitle").Value,
                                        ObjectText = a.Element("textentry").Value,


                                        }   
                                         )


                      };
+3  A: 

You can't easily do it with a query expression - at least not without a horrible side effect. However, you can easily do it with dot notation for either Select or Where. Given that you've got quite a long query expression, it's probably easiest to embed an extra call to where at the start - assuming you do actually want the index of "s" in the original expression:

var AllSections = 
  from s in xmlDoc.Descendants("section")
  select new
  {
      id = s.Attribute("id").Value,
      themeTitle = s.Element("themeTitle").Value,
      themeText = s.Element("themeText").Value,
      objects = (from  a in AllObjects.Select((Item,Index) => new {Item,Index})
                 join b in s.Item.Descendants("object")
                 on  a.Item.Attribute("accessionNumber").Value equals
                     b.Attribute("accessionNumber").Value
                  //select a
                  select new
                  {
                    //index = insert ordinal id/index of element
                    Index = a.Index,
                    ObjectTitle = a.Element("ObjectTitle").Value,
                    ObjectText = a.Element("textentry").Value,
                  }   
                )
  };

That's assuming you want the index of a within AllObjects.

Jon Skeet
I would like index of a.
Salt Packets
Okay, editing...
Jon Skeet
+1  A: 

@Jon Skeet gave you the appropriate overload of Select to use, and here is it in your query:

var AllSections = from s in xmlDoc.Descendants("section")
    select new
    {
        id = s.Attribute("id").Value,
        themeTitle = s.Element("themeTitle").Value,
        themeText = s.Element("themeText").Value,
        objects = (from a in AllObjects 
                   join b in s.Descendants("object")
                       on a.Attribute("accessionNumber").Value
                       equals b.Attribute("accessionNumber").Value
                   select a).Select((a, index) =>
                       new
                       {
                           Index = index,
                           ObjectTitle = a.Element("ObjectTitle").Value,
                           ObjectText = a.Element("textentry").Value,
                       })
    };
sixlettervariables
That's giving the index *within the join* rather than the index of `a` within `AllObjects`. That *may* be what's required, of course... but I've edited my answer to give `a`'s index.
Jon Skeet
Good catch, I couldn't quite figure out which one he wanted.
sixlettervariables