Is there a way I can determine which row I am on in a foreach loop?

  • A+
Category:Languages

I have this code:

foreach (var row in App.cardSetWithWordCounts) {    details.Children.Add(new SeparatorTemplate());    // do some tasks for every row     // in this part of the loop ... } 

I would like to not do the adding of a SeparatorTemplate BUT I would like to do the other tasks on the first run of the foreach. Does anyone have a suggestion on how I can do this?

I want to execute the rest of the code in the foreach but not the line adding the template on the first time around.

 


If you want to skip the first row, you can use Skip:

foreach (var row in App.cardSetWithWordCounts.Skip(1)) 

If you want to know the exact row number, use the Select overload:

foreach (var x in App.cardSetWithWordCounts.Select((r, i) => new { Row = r, Index = i }) {     // use x.Row and x.Index } 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: