0

if I have a foreach loop that takes a whole bunch of addresses and loops through them, is there a way I could skip the first 500 entries,

something like:

foreach(var row in addresses)
{
  string strAddr = row.ADDRESS + "," + row.CITY + "," + row.ST;
  System.Threading.Thread.Skip(500)
}

I know skip doesn't exist but is there anything I can use that would do the same thing?

  • 1
    Use `Enumerable.Skip` like `foreach(var row in addresses.Skip(500))` – Habib Nov 19 '14 at 15:11
  • 1
    Do you want to skip ONLY the first 500 entries or take every 500th item? – DavidG Nov 19 '14 at 15:12
  • possible duplicate of [LINQ: How to skip one then take the rest of a sequence](http://stackoverflow.com/questions/2431744/linq-how-to-skip-one-then-take-the-rest-of-a-sequence) – Habib Nov 19 '14 at 15:12
  • Note: `.Skip(int value)` is available on `IEnumerable` when you add `using System.Linq`. – Silvermind Nov 19 '14 at 15:14
  • only the first 500 need to be skipped – RyeNyeTheComputerScienceGuy Nov 19 '14 at 15:16
  • I believe that you want to skip after certain conditions? and that's the reason why you have your skip in the pseudo code on the last line of the loop right? In that case, use a for loop instead of foreach and increase the loop counter variable with 500. In other words, what is the reason that you want to skip? is it to speed up? if so, you can also use the parralel for loop. – JP Hellemons Nov 19 '14 at 15:16
  • Then the answer from @TimSchmelter is what you need – DavidG Nov 19 '14 at 15:17

4 Answers4

6

You can use a method with a meaningful name:

foreach(var row in addresses.Skip(500))
{
    // ...
}

You need to add using System.Linq; since it's a LINQ extension method.

If the type of addresses doesn't implement the generic IEnumerable<T> interface you could use Cast<T>. For example (presuming the type is Address):

foreach(var row in addresses.Cast<Address>().Skip(500))
{
    // ...
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • However, since he is using `foreach(var row` and row is apparently not of type `object`, we can assume that addresses implements `IEnumerable
    `
    – James Curran Nov 19 '14 at 15:40
2

You can use the Skip extension method:

foreach(var row in addresses.Skip(500))
{
    string strAddr = row.ADDRESS + "," + row.CITY + "," + row.ST;
}

Or, if addresses is just an array or list:

for (int i = 500 ; i < addresses.Count ; i++) // assuming addresses is a List<T>
{
    var row = addresses[i];
    string strAddr = row.ADDRESS + "," + row.CITY + "," + row.ST;
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
1

Use the Enumerator interface directly: http://msdn.microsoft.com/en-us/library/system.collections.ienumerator(v=vs.110).aspx

var enumerator = addresses.GetEnumerator();
for (var i = 0; i < 300 && enumerator.MoveNext(); i++);
Andrius Bentkus
  • 1,392
  • 11
  • 25
1

A feasible and fast solution would be to use LINQ Skip method to iterate over a subset of the collection starting from the 500th element of the collection.

foreach (var row in addresses.Skip(500))
{
   // do your stuff...
}
Mauro
  • 90
  • 9