c# - Get data out of foreach loop -


i need try , string itemout out of foreach loop. how can achieve this? tried refactoring method no success.

var modaltypeitemdata = modalgpdata.descendants(occ + "itemdata")                         .where(x => (string)x.attribute("itemoid") == "i_table_modal_type_table")                         .select(_ => _.attribute("value").value); foreach (var modaltypeitem in modaltypeitemdata) {     row["modality type"] = modaltypeitem;     string itemout = modaltypeitem; } 

you can itemout out of loop code:

string itemout = null; foreach (var modaltypeitem in modaltypeitemdata) {     row["modality type"] = modaltypeitem;     itemout = modaltypeitem; } 

but give modal type item of last element. i'm not sure if need.

if need items, need list<string> or array (string[]). done with:

var itemsout = modaltypeitemdata.select(item => item.modaltypeitem).tolist(); 

Comments