LINQ ToList() causes all records to be the last one off a coroutine -
i seem have misunderstanding because following code works correctly if don't append tolist() command:
ienumerable<processorinfo> query = ( n in infoget(emachineinfodepth.logicalprocessor) select n ) .tolist();
infoget looks this:
internal static ienumerable<processorinfo> infoget(emachineinfodepth depth) { processorinfo result = new processorinfo(); // loop through workgroups foreach (workgroup wg in workgroups_s) { result.workgroup = wg; if (depth >= emachineinfodepth.numanode) { // loop through numanodes foreach (numanode node in wg.numanodes) { result.numanode = node; if (depth >= emachineinfodepth.cpu) { // loop through cpus foreach (cpu cpu in node.cpus) { result.cpu = cpu; if (depth >= emachineinfodepth.core) { // loop through cores foreach (core core in cpu.cores) { result.core = core; if (depth >= emachineinfodepth.logicalprocessor) { // loop through logicalprocessors foreach (logicalprocessor lp in core.logicalprocessors) { result.logicalproc = lp; yield return result; } } else { yield return result; } } } else { yield return result; } } } else { yield return result; } } } else { yield return result; } } }
with tolist() correct count, records equal final element in sequence. while variable scope error in complex coroutine, in iterations see final value, why code work without tolist()?
my question is: misunderstanding?
problem is, you're returning reference same variable time:
processorinfo result = new processorinfo();
that's place you're creating new processorinfo
object. change it's properties values later, return still same object.
you should consider adding copy constructor processorinfo()
class, , replace every yield return result;
call yield return new processorinfo(result);
. easiest way make work.
update
it look works e.g. when you've saved variable state somewhere during loop:
foreach(var item in query) { itemslist.add(item); propertylist.add(item.intproperty); }
after call itemslist
contain incorrect data, while propertylist
fine.
Comments
Post a Comment