Refresh object in IEnumerable <> not updating?

I think today I have my own head: I have an IEnumerable POCO type containing about 80,000 rows and a db table (L2E / EF4) containing a subset of the rows where there was an "error / difference" (about 5,000 rows, but often repeats to give about 150 individual entries * strong text *)

The following code gets a separate VSACode "by mistake" and then tries to update the full set of results by updating the JUST lines that match ... but that doesn't work!

What nonsense 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);

After this code i = 0

I also tried:

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

How to update the "results" to set only the corresponding Difference property?

+5
3

, results - ( ), , . , . , .

results - , ToList() - results , .

+10

. , linq , , .

Pro LINQ Language Integrated Query # 2010 ":

" , , , . , . , . . , , , IEnumerable, , ToArray, ToList, ToDictionary ToLookup, , ."

:

http://blogs.msdn.com/b/charlie/archive/2007/12/09/deferred-execution.aspx

+2

@jonskeet...

, , , , - .

- .

ToList() , , , , , . - , referencing .

, , .

public class Widget
{
    public string Name { get; set; }
}

var widgets1 = new[]
{
    new Widget { Name = "Red", },
    new Widget { Name = "Green", },
    new Widget { Name = "Blue", },
    new Widget { Name = "Black", },
};

// adding ToList() will result in 'static' query result but
// updates to the objects will still affect the source objects
var query1 = widgets1
    .Where(i => i.Name.StartsWith("B"))
    //.ToList()
    ;

foreach (var widget in query1)
{
    widget.Name = "Yellow";
}

// produces no output unless you uncomment out the ToList() above
// query1 is reevaluated and filters out "yellow" which does not start with "B"
foreach (var name in query1)
    Console.WriteLine(name.Name);

// produces Red, Green, Yellow, Yellow
// the underlying widgets were updated
foreach (var name in widgets1)
    Console.WriteLine(name.Name);
0

All Articles