在 foreach 循环内修改对象的属性不起作用?
这让我很困惑。我使用 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 属性设置正确。
但在循环结束时的断点中,它没有保存它?
我很困惑,索引不应该是对内存中正在修改的位置的引用吗?毕竟它是一个物体。我在这里缺少什么?
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.
but then in a breakpoint at the end of the loop, it didn't save it?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
IEnumerable 实现是什么?它会返回对象的副本吗?
RetreaveIndex 是一个结构体,因此也是一个值类型吗?如果是这样,那么变量
index
将是一个副本。What is the IEnumerable implementation? Could it be returning a copy of the object?
Is RetreaveIndex a struct, and thus a value type? If so, then the variable
index
will be a copy.根据传入的 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.
来自项目网站:
换句话说,
Query
每次都会从后备存储中重新加载值,并且在枚举后不会保留项目。当您在循环结束后再次查看某个项目时,该项目将从后备存储中重新加载。From the project web site:
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.实际上,发生的事情是您误解了
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 fromyield return
, the code after thatyield return
is being executed. In technical terms,yield return
is doing lazy evaluation. So in the net effect of yourforeach
loop is that it's calling the code to make the item in theIEnumerable
for however many items are in thatIEnumerable
.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