Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update item in IEnumerable

Tags:

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?

like image 915
nik Avatar asked Apr 16 '15 13:04

nik


People also ask

Can I modify IEnumerable?

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.

How do I add items to an IEnumerable list?

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");

What is IEnumerable C#?

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.

How do I find the value of an IEnumerable list?

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.


2 Answers

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.

like image 159
D Stanley Avatar answered Oct 02 '22 07:10

D Stanley


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"; 
like image 23
tehmas Avatar answered Oct 02 '22 07:10

tehmas