0

I am running a process like this:

foreach (var x in App.cardSetWithWordCount.Select((r, i) => 
      new { Row = r, Index = i }))
{
            // Some code here
}

The code inside this routine takes a long time and what I would like to do is to run this for the first ten rows and then run it again for the remaining rows. Something like this:

// just the first ten rows in App.cardSetWithWordCount
foreach (var x in App.cardSetWithWordCount.Select((r, i) => 
      new { Row = r, Index = i }))
{
            // Some code here
}

then

// everything else except ten rows in App.cardSetWithWordCount
foreach (var x in App.cardSetWithWordCount.Select((r, i) => 
      new { Row = r, Index = i }))
{
            // Some code here
}

Is there a way to select just the first ten rows and also a way to skip the first ten that I could apply to the foreach?

5
  • What have you tried so far ?
    – Fabjan
    Nov 21, 2018 at 8:41
  • as far as i understand you can not do. if i understand correctly. You want to change the list during iteration? Nov 21, 2018 at 8:46
  • you can add a where a Where() statement like so .Where(x => x.r <=10)
    – styx
    Nov 21, 2018 at 8:49
  • Consider using MoreLINQ's Segment.
    – mjwills
    Nov 21, 2018 at 8:58
  • You also need to put a .ToList() at the end.
    – roozbeh S
    Nov 21, 2018 at 9:12

1 Answer 1

3

You can use

App.cardSetWithWordCount.Take(10)

To get the first 10, and then

App.cardSetWithWordCount.Skip(10)

To skip the first ten

Or I guess you could do it the old fashioned way, iterating over App.cardSetWithWordCount with a for loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.