c# - Cannot read from a closed textreader after iterating through IEnumerable once -


i've got problem "cannot read closed textreader" exception. have program supposed data file, funny stuff , timespans of everything.

first in 1 function, i'm loading data:

ienumerable<string> somevariable; somevariable = (from _something in file.readlines(@"file location") select _something.split(' ')[1] ); 

in second function, run through once :

foreach (var _something in somevariable) {     somelist.add(_something); } 

hovewer in third function, when try run throgh again:

foreach (var _something in somevariable) {     someotherlist.add(_something); } 

i exception: cannot read closed textreader. looks after 1 enumeration, no longer possible iterate through somevariable.

is problem ienumerable itself, or have file.readlines function? find silly reload data file every time want iterate through it...

ps. did research , aware 1 of sollutions put both actions in 1 function, unfortunatelly university program , have write in way - have separate timespans of both actions.

you are, in fact, reloading data each time iterate through somevariable enumerable. select clause in linq corresponds enumerable.select:

this method implemented using deferred execution. immediate return value object stores information required perform action. query represented method not executed until object enumerated [for example] using foreach.

this presumably not intended behaviour; somevariable eagerly populated resulting list of strings. achieve this, can call tolist on enumerable:

ilist<string> somevariable =  (     _something in file.readlines(@"file location")      select _something.split(' ')[1] ).tolist(); 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -