I need to update a value in a IEnumerable list.
Here is a brief IEnumerable example:
IEnumerable<string> allsubdirs = new List<string>() { "a", "b", "c" };
Now if I want to add a timestamp to each item, this doesnt work:
allsubdirs.Select(a => a = a + "_" + DateTime.Now.ToString("hhmmss")).ToList();
Neither does this:
foreach (var item in allsubdirs)
item = item + "_" + DateTime.Now.ToString("hhmmss");
I made it work like this:
IEnumerable<string> newallsubdirs = allsubdirs.Select(a => a + "_" + DateTime.Now.ToString("hhmmss")).ToList();
allsubdirs = newallsubdirs;
but this somehow seems like cheating. Whats the proper way of doing this please?
IEnumerable
is readonly. You can not modify it. If you want to modify collection, then consider to change it toList
. – Farhad Jabiyev Apr 16 '15 at 13:29Select
but throws them away because they are never stored – Panagiotis Kanavos Apr 16 '15 at 13:33