2

如何在此 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,


                                        }   
                                         )


                      };
4

2 回答 2

4

您不能使用查询表达式轻松地做到这一点 - 至少不会没有可怕的副作用。但是,您可以轻松地使用点符号来完成SelectWhere。鉴于您有一个相当长的查询表达式,最简单的方法可能是在开始时嵌入额外的 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,
                  }   
                )
  };

那是假设您想要awithin的索引AllObjects

于 2009-09-09T16:55:21.697 回答
2

@Jon Skeet 为您提供了适当的Select重载以供使用,这是您的查询中的内容:

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,
                       })
    };
于 2009-09-09T17:13:29.510 回答