IEnumerable 集合中元素的序号位置 (Linq to XMl )

发布于 2024-08-04 05:29:46 字数 1173 浏览 1 评论 0原文

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


                                        }   
                                         )


                      };

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,


                                        }   
                                         )


                      };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨离汐 2024-08-11 05:29:46

您无法轻松地使用查询表达式来完成此操作 - 至少会产生可怕的副作用。但是,您可以使用 SelectWhere 的点表示法轻松完成此操作。假设您有一个相当长的查询表达式,那么在开头嵌入一个额外的调用可能是最简单的 - 假设您确实想要原始表达式中“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,
                  }   
                )
  };

假设您想要 < 的索引AllObjects 中的 code>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.

画离情绘悲伤 2024-08-11 05:29:46

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

@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,
                       })
    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文