I need to modify a List<int> by indexes. I have the indexes in another List<int>
List<int> values;
List<int> indexes;
indexes.Select(i=>values[i]).ForEach(val=>val = 0);
Of course, I know the above syntax wont work. However, as I know the indexes of the items that I need to modify, I would like to to modify the list with the indexes with lambda. Is it possible to do that?
In some cases, LINQ isn’t going to give you a particularly better solution, especially when working with indices or when modifying data. I think the following simple
foreachloop will be probably the most readable way to write this:However, if the collection
indexesis of typeList<T>then you can use theForEachmethod (see MSDN). This makes the code a little shorter, but it is a question whether it is more readable than simpleforeachloop:ForEachmethod is not currently available in LINQ (for generalIEnumerable<T>), but there are quite a few people asking for it. But as I said, it is not clear whether it makes code more readable or not.(If you wanted to write the same thing for
IEnumerable<T>you’d have to either useToListor write your ownForEachextension method).