-1

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?

James
  • 1,827
  • 5
  • 39
  • 69
  • 1
    How about you set some breakpoints and start debugging? If your last "rowsPerColumn" contains a lower value than the rest, this will ofset the last page... If it's `4`, then your `skip` will be `4 * 3` which is 12, so that will indeed show records from the previous page where the skip was `5 * 2`. Explain what this logic (`rowsPerColumn[i] * page`) is supposed to do, otherwise the answer will be _"skip a fixed number of records"_. – CodeCaster Dec 29 '15 at 11:43
  • Take a look at this question: http://stackoverflow.com/questions/11463734/split-a-list-into-smaller-lists-of-n-size – Yacoub Massad Dec 29 '15 at 11:43
  • what is that count of GroupedLinksCategory? – Anik Saha Dec 29 '15 at 11:47
  • @Anik1991 Count is 19 – James Dec 29 '15 at 11:50
  • @CodeCaster rowsPerColumn is an array which holds 4 records as i shown in question. – James Dec 29 '15 at 11:53
  • That's not what I'm asking. **Why** are you basing your paging on how many records you have in the **current row**? You cannot skip less records for the last row if the last row happens to contain less records. You need to skip just as much as before. You may want to skip the highest number in the array, or if it can differ per row, keep count of how many you already displayed. – CodeCaster Dec 29 '15 at 11:54
  • @CodeCaster i followed this question to build Logic for paging http://stackoverflow.com/questions/9718117/selecting-first-10-records-then-next-10-paging-using-linq – James Dec 29 '15 at 11:54
  • @CodeCaster could you help me please on how to do it? – James Dec 29 '15 at 11:57

1 Answers1

1

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
     }
  }

}
Anik Saha
  • 4,313
  • 2
  • 26
  • 41