Порядковый номер элемента в IENumerable Collection (Linq to XMl) - PullRequest
2 голосов
/ 09 сентября 2009

Как мне вставить порядковый номер элемента в качестве его атрибута в этот запрос linq.

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,


                                        }   
                                         )


                      };

Ответы [ 2 ]

4 голосов
/ 09 сентября 2009

Вы не можете легко сделать это с помощью выражения запроса - по крайней мере, без ужасного побочного эффекта. Однако вы можете легко сделать это с точечной нотацией для Select или Where. Учитывая, что у вас довольно длинное выражение запроса, возможно, проще всего встроить дополнительный вызов в начало в начале - при условии, что вы действительно хотите индекс «s» в исходном выражении:

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,
                  }   
                )
  };

Предполагается, что вы хотите индекс a в пределах AllObjects.

2 голосов
/ 09 сентября 2009

@ Джон Скит дал вам соответствующую перегрузку Выберите для использования, и вот оно в вашем запросе:

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,
                       })
    };
...