linq 不会迭代列表

发布于 2025-01-03 13:29:43 字数 1266 浏览 0 评论 0原文

我编写了以下代码,

List<Pupils> pupils = PupilsDAO.SelectDAO();
XElement dtpupil = new XElement("DtDatas",
                from xlist in pupils
                orderby xlist.Id
                    select new XElement("DtData",
                        new XElement("ref", xlist.Id),
                        new XElement("forename", xlist.Forename),
                        new XElement("surname", xlist.Surname)
                    )
            );

而不是为列表中的每个元素获取不同的 XML 对象,我确实为列表中的每个项目获取了输出,但它们都是相同的,而不是实际迭代,因此只需加载比如说...

<DtDatas>
   <DtData>
      <ref>01</ref>
      <forename>joe</forename>
      <surname>bloggs</surname>
   </DtData>
 <DtData>
      <ref>01</ref>
      <forename>joe</forename>
      <surname>bloggs</surname>
   </DtData>
 <DtData>
      <ref>01</ref>
      <forename>joe</forename>
      <surname>bloggs</surname>
   </DtData>
 <DtData>
      <ref>01</ref>
      <forename>joe</forename>
      <surname>bloggs</surname>
   </DtData>
</DtDatas>

有人有什么想法吗?我是否打算为列表添加迭代?

I've written the following code

List<Pupils> pupils = PupilsDAO.SelectDAO();
XElement dtpupil = new XElement("DtDatas",
                from xlist in pupils
                orderby xlist.Id
                    select new XElement("DtData",
                        new XElement("ref", xlist.Id),
                        new XElement("forename", xlist.Forename),
                        new XElement("surname", xlist.Surname)
                    )
            );

rather than getting a different XML object for each element in the list, I do get an output for every item in the list, but they're all the same rather than actually iterating through the, so just loads of say...

<DtDatas>
   <DtData>
      <ref>01</ref>
      <forename>joe</forename>
      <surname>bloggs</surname>
   </DtData>
 <DtData>
      <ref>01</ref>
      <forename>joe</forename>
      <surname>bloggs</surname>
   </DtData>
 <DtData>
      <ref>01</ref>
      <forename>joe</forename>
      <surname>bloggs</surname>
   </DtData>
 <DtData>
      <ref>01</ref>
      <forename>joe</forename>
      <surname>bloggs</surname>
   </DtData>
</DtDatas>

Does anyone have any ideas? Am I meant to add an iteration for the list?

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

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

发布评论

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

评论(1

撩起发的微风 2025-01-10 13:29:43

您提供的代码很好。这表明您列表中的数据不正确。可能的选项:

  • 您多次向列表添加相同的引用,并在构建列表的任何循环中重复改变同一对象。
  • 您正在使用静态变量来支持属性

如果您可以发布 Pupils 类和 SelectDAO 方法,我们应该能够提供更多帮助。

The code you've presented is fine. That suggests that the data in your list is incorrect. Possible options:

  • You're adding the same reference to the list multiple times, and just mutating the same object repeatedly within whatever loop is constructing the list.
  • You're using static variables to back the properties

If you can post the Pupils class and the SelectDAO method, we should be able to help more.

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