Solution for GetRange error accessing elements off the list

-3

When calling the GetRange of a list, where it calls 10 in 10 list values at a time, however, it gives the following error:

  

Offset and length were out of bounds for the array or count greater   than the number of elements from index to the end of the source   collection.

As he calls 10 out of 10 if he finishes the list and does not contain the 10 he happens the error, how can I do that when calling 10 if he does not contain 10 within the list he takes the remainder to contain thus avoiding the error .

 foreach (var item in lst.GetRange(count, index))
        {
          //count e index e incrementado +10 quando passa novamente começa do (0,9)
         //aqui inseri os valores no cshtml
        }
    
asked by anonymous 04.10.2016 / 13:38

1 answer

0

The mistake is clear, as you yourself said. With .GetRange() you are passing the value higher than the amount of items in the list.

To continue using .GetRange() , you would have to treat for this not to occur.

However, from what you've explained in comments , I believe the . Take () and .Skip () can serve you better.

  

.Take ()
  Returns a specified number of contiguous elements at the beginning of a sequence.

     

.Skip ()
  Ignores a specified number of elements in a sequence and then returns the remaining elements.

An example with the usage would be:

lst.Skip(10).Take(10).ToList()

Where .Skip() is "skipping" 10 items and .Take() is selecting the next 10 items.

    
04.10.2016 / 15:41