2015-10-06 65 views
1

我为我的视图创建了一个分页系统,根据视图中显示的列值显示排序的前20项(如果总数少于20项,则更少),然后查看者可以单击“第2页”查看项目21至40(或更少,如果有小于40个),然后在“第3页”看到项41-60,等等。我控制器具有构件使用LINQ,我如何获得行M到M + N?

private static const int _scoresPerPage = 20; 

,我构建方法

public string getPageNRows (int N) 
    { 
     string scoresRowsHtml = String.Empty; 
     // ... 
     return scoresRowsHtml; 
    } 

其中回收信息。现在,我知道如何让0通过ScoresController._scoresPerPage * N,这样我就可以在技术上做类似

public string getPageNRows (int N) 
    { 
     string scoresRowsHtml = String.Empty; 
     IQueryable<Score> table1 = (from s in this._SD.Scores 
              orderby s.score1 descending 
              select s 
              ).Take(ScoresController._scoresPerPage * N); 
     IQueryable<Score> table2 = (from s in table1 
            orderby s.score1 ascending 
            select s).Take(ScoresController._scoresPerPage); 
     table2.Reverse(); 
     // ... 
     return scoresRowsHtml; 
    } 

,但显然这是荒谬的。我应该在这里做什么?

回答

2

你想Skip()与TAKE组合()

IQueryable<Score> table1 = (from s in this._SD.Scores 
          orderby s.score1 descending 
          select s 
          ).Skip(ScoresController._scoresPerPage * (N-1)) 
          ).Take(ScoresController._scoresPerPage); 

假设n ==可1指的第一页。