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?
As mentioned in the existing answers, an IEnumerable collection cannot be modified by trying to assign a new value to one of its elements. However if the elements contained in the collection are of a mutable type, you could modify them.
What you can do is use the Add extension method to create a new IEnumerable<T> with the added value. var items = new string[]{"foo"}; var temp = items; items = items. Add("bar");
IEnumerable in C# is an interface that defines one method, GetEnumerator which returns an IEnumerator interface. This allows readonly access to a collection then a collection that implements IEnumerable can be used with a for-each statement.
We can get first item values from IEnumerable list by using First() property or loop through the list to get respective element. IEnumerable list is a base for all collections and its having ability to loop through the collection by using current property, MoveNext and Reset methods in c#, vb.net.
Linq is for querying, not updating. Linq queries return a new collection based on projections, filters etc. So your choices are:
Save the "new" collection back to the variable (or a new variable, if necessary):
allsubdirs = allsubdirs.Select(a => a = a + "_" + DateTime.Now.ToString("hhmmss")).ToList();
Use a writable interface like IList<T>
and a for
loop:
IList<string> allsubdirs = new List<string>() { "a", "b", "c" }; for(int i=0; i<allsubdirs.Count(); i++) allsubdirs[i] = allsubdirs[i] + "_" + DateTime.Now.ToString("hhmmss");
The main difference is that Select
does not modify the original collection, while the for
loop does.
My opinion is that the Select
is cleaner and is not "cheating" - you're just adding a projection on top of the original collection.
D Stanley has answered it correctly. I would like to add to his answer since the title is Update item in IEnumerable
implying only a single item is to be updated.
As D Stanely explained in his answer:
Linq is for querying, not updating.
and
Use a writable interface like IList and a for loop
For updating a single item, you can retrieve the index of the item to be updated and use that index to update it.
For example:
IList<string> allsubdirs = new List<string>() { "a", "b", "c" }; int index = allsubdirs.IndexOf("a"); allsubdirs[index] = "d";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With