在 foreach 循环内修改对象的属性不起作用?

发布于 2024-11-09 05:29:17 字数 679 浏览 0 评论 0原文

这让我很困惑。我使用 PetaPoco 从数据库中检索一些值,然后循环它们并检索一个值以分配给每个对象的属性之一。

    public IEnumerable<RetreaveIndex> FillResults(IEnumerable<RetreaveIndex> results)
    {
        //add the associated users
        foreach (RetreaveIndex index in results)
        {
            index.AssociatedUsers = _registeredUserDao.GetUsersByIndex(index).ToList();
        }
        return results;
    }

当我在 foreach 循环期间设置断点时,AssociatedUsers 属性设置正确。 during foreach循环

但在循环结束时的断点中,它没有保存它? 在此处输入图像描述

我很困惑,索引不应该是对内存中正在修改的位置的引用吗?毕竟它是一个物体。我在这里缺少什么?

This is puzzling me. I'm using PetaPoco to retreive some values from a database, and then looping over them and retrieving a value to assign to one of the properties of each object.

    public IEnumerable<RetreaveIndex> FillResults(IEnumerable<RetreaveIndex> results)
    {
        //add the associated users
        foreach (RetreaveIndex index in results)
        {
            index.AssociatedUsers = _registeredUserDao.GetUsersByIndex(index).ToList();
        }
        return results;
    }

When I set a breakpoint during the foreach loop, the AssociatedUsers property is being set correctly.
during foreach loop

but then in a breakpoint at the end of the loop, it didn't save it?
enter image description here

I'm confused, shouldn't Index be a reference to a place in memory which is being modified? It's an object after all. What am I missing here?

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

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

发布评论

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

评论(4

孤独岁月 2024-11-16 05:29:17
  1. IEnumerable 实现是什么?它会返回对象的副本吗?

  2. RetreaveIndex 是一个结构体,因此也是一个值类型吗?如果是这样,那么变量 index 将是一个副本。

  1. What is the IEnumerable implementation? Could it be returning a copy of the object?

  2. Is RetreaveIndex a struct, and thus a value type? If so, then the variable index will be a copy.

江南月 2024-11-16 05:29:17

根据传入的 IEnumerable 的实现方式,它不要求下次枚举数据时返回与之前相同的对象。尝试在 foreach 循环之前将 IEnumerable 转换为 List 并返回该列表。

Depending on how the IEnumerable passed in is implemented, it has no requirement that the next time it enumerates over the data that it return the same objects as before. Try turning the IEnumerable into a List before the foreach loop and return that insead.

星光不落少年眉 2024-11-16 05:29:17

来自项目网站

查询与获取

Database 类有两个方法
检索记录查询和获取。
这些几乎相同,除了
Fetch 返回一个 List<> POCO 的
而查询使用yield返回
迭代结果,无需
将整个集合加载到内存中。

换句话说,Query 每次都会从后备存储中重新加载值,并且在枚举后不会保留项目。当您在循环结束后再次查看某个项目时,该项目将从后备存储中重新加载。

From the project web site:

Query vs Fetch

The Database class has two methods for
retrieving records Query and Fetch.
These are pretty much identical except
Fetch returns a List<> of POCO's
whereas Query uses yield return to
iterate over the results without
loading the whole set into memory.

In other words, Query re-loads the values from the backing store each time, and doesn't keep an item around after it's been enumerated. When you go look at an item again after the end of your loop, that item is re-loaded from the backing store.

南汐寒笙箫 2024-11-16 05:29:17

实际上,发生的事情是您误解了 yield return 语法的输出。

发生的情况是,当您迭代从 yield return 返回的 IEnumerable 时,将执行 yield return 之后的代码。用技术术语来说,yield return 正在做惰性评估。因此,foreach 循环的最终效果是,它调用代码来使该项目位于 IEnumerable 中,无论该 IEnumerable 中有多少项目>。

CodeProject 的以下帖子很好地解释了这种行为:
http://www.codeproject.com/Articles/38097 /收益回报背后的秘密.aspx

Actually, what is going on is that you are misinterpreting the output from the yield return syntax.

What's happening is that as you iterate over the IEnumerable returned from yield return, the code after that yield return is being executed. In technical terms, yield return is doing lazy evaluation. So in the net effect of your foreach loop is that it's calling the code to make the item in the IEnumerable for however many items are in that IEnumerable.

The following post from CodeProject does an excellent job of explaining this behavior:
http://www.codeproject.com/Articles/38097/The-Mystery-Behind-Yield-Return.aspx

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