Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List<T>.ForEach with index

Tags:

I'm trying to find the LINQ equivalent of the following code:

NameValueCollection nvc = new NameValueCollection();  List<BusinessLogic.Donation> donations = new List<BusinessLogic.Donation>(); donations.Add(new BusinessLogic.Donation(0, "", "", ""); donations.Add(new BusinessLogic.Donation(0, "", "", ""); donations.Add(new BusinessLogic.Donation(0, "", "", "");  for(var i = 0; i < donations.Count(); i++) {     // NOTE: item_number_ + i - I need to be able to do this     nvc.Add("item_number_" + i, donations[i].AccountName); } 

I was hoping I could use something like:

NameValueCollection nvc = new NameValueCollection();  List<BusinessLogic.Donation> donations = new List<BusinessLogic.Donation>(); donations.Add(new BusinessLogic.Donation(0, "", "", ""); donations.Add(new BusinessLogic.Donation(0, "", "", ""); donations.Add(new BusinessLogic.Donation(0, "", "", "");  donations.ForEach(x => nvc.Add("item_name_" + ??, x.AccountName); 

But I've not found a way to determine which iteration the loop is on. Any help would be appreciated!

like image 285
James Hill Avatar asked Oct 24 '12 17:10

James Hill


People also ask

What is index in foreach?

Luckily, there are several ways to get an index variable with foreach : Declare an integer variable before the loop, and then increase that one inside the loop with each loop cycle. Create a tuple that returns both the element's value and its index. Or swap the foreach loop with the for loop.

How do I get current index in foreach?

With the later C# versions so you also use tuples, so you'll have something like this: foreach (var (item, i) in Model. Select((v, i) => (v, i))) Allows you to access the item and index (i) directly inside the for-loop with tuple deconstruction.

Can you use foreach on a list?

forEach statement is a C# generic statement which you can use to iterate over elements of a List.

Can we obtain the array index using foreach loop in C#?

The following example uses type inference when deconstructing the list of 2-tuples returned by the Select() method. Now we can directly access the index and the value fields inside the loop. That's all about finding the current index in a foreach loop in C#.


1 Answers

LINQ doesn't have a ForEach method, and for good reason. LINQ is for performing queries. It is designed to get information from some data source. It is not designed to mutate data sources. LINQ queries shouldn't cause side effects, which is exactly what you're doing here.

The List class does have a ForEach method, which is what you are using. Because it's not actually in the System.Linq namespace it's not technically a part of LINQ.

There is nothing wrong with the for loop in your question. It would be wrong (from a good practice perspective) to try to change it in the way that you're trying to.

Here is a link that discusses the matter in more detail.

Now, if you want to ignore that advice and use a ForEach method anyway, it's not hard to write one that provides an index to the action:

public static void ForEach<T>(this IEnumerable<T> sequence, Action<int, T> action) {     // argument null checking omitted     int i = 0;     foreach (T item in sequence)     {         action(i, item);         i++;     } } 
like image 190
Servy Avatar answered Sep 22 '22 13:09

Servy