Create new List with for loop in C# and VB.Net

Last Reply 10 months ago By pandeyism

Posted 10 months ago

I have 2742 records

I want to divide this into 100 parts and use the same in a for loop.

how to set the start and end values in the sequenece

seq1: 1 to 100
seq2:101 to 200
etc

 

 

 

You are viewing reply posted by: pandeyism 10 months ago.
Posted 10 months ago

Hi Jayakumar,

Please refer below sample.

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    List<List<int>> lists = new List<List<int>>();
    List<int> list = new List<int>();
    for (int i = 1; i <= 2742; i++)
    {
        list.Add(i);
        if (i % 100 == 0)
        {
            lists.Add(list);
            list = new List<int>();
        }
    }
    lists.Add(list);
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim lists As List(Of List(Of Integer)) = New List(Of List(Of Integer))()
    Dim list As List(Of Integer) = New List(Of Integer)()
    For i As Integer = 1 To 2742
        list.Add(i)
        If i Mod 100 = 0 Then
            lists.Add(list)
            list = New List(Of Integer)()
        End If
    Next
    lists.Add(list)
End Sub