Search code examples
c#listpagination

Getting duplicate records while paging in list


I am looping on list which has 19 records. I want to get records in 4 pages. First page will have 5 records, second will have 5 , Third will have 5 and fourth will have 4 records.

So i am looping as below

for (int i = 0; i < totalColumnToShow; i++)
{

      var page = i + 1;
      var skip = rowsPerColumn[i] * (page - 1);
      PagedList =GroupedLinksCategory.Select(y => new copiedList { Title = y.Title, ID = y.ID, Name= y.Name }).Skip(skip).Take(rowsPerColumn[i]).ToList();

      foreach (var row in PagedList)
      {
         if (row.Name== "Test")
         {
            //my Logic
         }
         else
         {
             //my Logic
         }
      }

 }

variable rowsPerColumn is array holding 4 records like

rowsPerColumn[0] = 5 rowsPerColumn[1]= 5 rowsPerColumn[2] = 5 rowsPerColumn[3]= 4

On last page i am getting 2 records from page 3. What is wrong With my Logic?


Solution

  • At last loop when i=3

    skip is equal to 12 instead of 15. So thats why you get repeated value.

    int Totalskip=0;
    for (int i = 0; i < totalColumnToShow; i++)
    {
    
      if(i>0){
      var skip = rowsPerColumn[i-1];
      Totalskip =Totalskip+ skip;
      }
      PagedList =GroupedLinksCategory.Select(y => new copiedList { Title = y.Title, ID = y.ID, Name= y.Name }).Skip(Totalskip).Take(rowsPerColumn[i]).ToList();
    
      foreach (var row in PagedList)
      {
         if (row.Name== "Test")
         {
            //my Logic
         }
         else
         {
             //my Logic
         }
      }
    
    }