2
 lowerbound = (CurrentPage - 1) * 10;
 upperbound = (CurrentPage * 10) -1;

I have an upper bound and lower bound two integers that specify what is the lowest and highest element between which elements from the List have to be accessed

List<string> take = list.Take(upperbound).ToList();

How do I select items from a list lower bound to upper bound?

foreach (string elemt in take)
{
    HtmlGenericControl div = new HtmlGenericControl("div");
    div.ID = "div" + elemt;

    Label text = new Label();
    text.Text = elemt;
    div.Controls.Add(text);
    divtest.Controls.Add(div);
}
yoozer8
  • 7,361
  • 7
  • 58
  • 93
vini
  • 4,657
  • 24
  • 82
  • 170
  • Possible duplicate: http://stackoverflow.com/questions/1301316/c-sharp-equivalent-of-python-slice-operation – ean5533 May 20 '13 at 15:58

5 Answers5

9
IEnumerable<TSource>.Skip(lowerBound).Take(upperBound-lowerBound)

MSDN documentation for Skip and Take.

Boris B.
  • 4,933
  • 1
  • 28
  • 59
5

Use List's GetRange method

 take.GetRange(lowerbound, upperbound - lowerbound + 1);
ean5533
  • 8,884
  • 3
  • 40
  • 64
I4V
  • 34,891
  • 6
  • 67
  • 79
3

Try:

list.Skip(lowerBound).Take(upperbound - lowerbound);
Ben Reich
  • 16,222
  • 2
  • 38
  • 59
1

Use LINQ

For items between lowerbound and upperbound

list.Skip(lowerbound).Take(upperbound - lowerbound);

Or if you have paging use

list.Skip((PageNumber - 1) * PageSize).Take(PageSize);
arunlalam
  • 1,838
  • 2
  • 15
  • 23
0
for(int i = lowerBound; i <= upperBound; ++i)
{
   string elemt = take(i);
}
Moo-Juice
  • 38,257
  • 10
  • 78
  • 128