在IEnumerable的更新&lt对象;>没有更新? [英] Update object in IEnumerable<> not updating?

查看:153
本文介绍了在IEnumerable的更新&lt对象;>没有更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得我有我的今天stupuid头:
我有一个包含含那里有行的一个子集,约80000行
和一个数据库表(L2E / EF4)一个POCO类型的IEnumerable一个错误/差异(约5000行,但经常反复给约150个不同的条目*的强大的文本的*)

I think I have my stupuid head on today: I have an IEnumerable of a POCO type containing around 80,000 rows and a db table (L2E/EF4) containing a subset of rows where there was a "an error/a difference" (about 5000 rows, but often repeated to give about 150 distinct entries*strong text*)

下面的代码获取不同VSACode的错误,然后尝试更新的完整结果集,只更新匹配的行...但它不工作!

The following code gets the distinct VSACode's "in error" and then attempts to update the complete result set, updating JUST the rows that match...but it doesn't work!

什么的愚蠢,我做了?!

What stupidity have I done?!

var vsaCodes = (from g in db.GLDIFFLs
  select g.VSACode)
  .Distinct();

foreach (var code in vsaCodes)
 {
  var hasDifference = results.Where(r => r.VSACode == code);
  foreach (var diff in hasDifference)
   diff.Difference = true;
 }

 var i = results.Count(r => r.Difference == true);



该代码后,我= 0

After this code, i = 0

我也试过:

foreach (var code in vsaCodes)
            {
                results.Where(r => r.VSACode == code).Select(r => { r.Difference = true; return r; }).ToList();
            }



我怎样才能更新了结果,设置只有匹配的差异的财产?

How can I update the "results" to set only the matching Difference property?

推荐答案

假设结果只是一个查询(您还没有表现出它),它会在每次迭代它的时间进行评估。如果查询创建新的对象的时候,你的不会的看到更新。如果返回引用相同的对象,你会的。

Assuming results is just a query (you haven't shown it), it will be evaluated every time you iterate over it. If that query creates new objects each time, you won't see the updates. If it returns references to the same objects, you would.

如果您更改结果是一个的物化的查询结果 - 例如,加入了ToList()来结束 - 那么循环访问结果不会的问题一个新的查询,你会看到你的变化。

If you change results to be a materialized query result - e.g. by adding ToList() to the end - then iterating over results won't issue a new query, and you'll see your changes.

这篇关于在IEnumerable的更新&lt对象;>没有更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆