title: .NET MVC Linq pagination with PagedList
body: <p>In my MVC app I'm using LINQ to retrieve data from DB and <a href="https://github.com/troygoode/PagedList" rel="nofollow">PagedList</a> for pagination. I have a couple of questions, after the code block, that I would like some help with.</p>
  
  <p><strong>Function where I retrieve data from cache or DB:</strong></p>
  
  <pre><code>public NewsPagedListDTO GetNewsFromCacheOrDB(int pageSize, int? newsID, int? page, string newsTitle, int? categoryID, int? orderByTitle, int? orderByPublisher, int? orderByDate, int? orderByCategory)
              {
              DataCache cache = new DataCache("default");
              object cacheNews = cache.Get("cacheNews");
  
              List&lt;News&gt; news = new List&lt;News&gt;();
  
  
              if (cacheNews == null)
              {
                  news = (from n in DB.News
                          select n).ToList();
  
              //Only cache if no parameters was provided
              if (newsID == null &amp;&amp; newsTitle == null &amp;&amp; categoryID == null &amp;&amp; orderByTitle == null &amp;&amp; orderByPublisher == null &amp;&amp;
                  orderByDate == null &amp;&amp; orderByCategory == null)
                  cache.Add("cacheNews", news);
              }
              }
              else
              {
                  news = (List&lt;News&gt;)cacheNews;
              }
  
              if (newsID != null)
                  news = news.Where(n =&gt; n.NewsID == newsID).ToList();
  
              if (categoryID != null)
                  news = news.Where(n =&gt; n.CategoryID == categoryID).ToList();
  
              if (newsTitle != null)
                  news = news.Where(n =&gt; n.Title == newsTitle).ToList();
  
              if (orderByTitle != null)
                  if (orderByTitle == 0)
                      news = news.OrderBy(n =&gt; n.Title).ToList();
                  else
                      news = news.OrderByDescending(n =&gt; n.Title).ToList();
  
              if (orderByPublisher != null)
                  if (orderByPublisher == 0)
                      news = news.OrderBy(n =&gt; n.PublishedByFullName).ToList();
                  else
                      news = news.OrderByDescending(n =&gt; n.PublishedByFullName).ToList();
  
              if (orderByDate != null)
                  if (orderByDate == 0)
                      news = news.OrderByDescending(n =&gt; n.DatePublished).ToList();
                  else
                      news = news.OrderBy(n =&gt; n.DatePublished).ToList();
  
              if (orderByCategory != null)
                  if (orderByCategory == 0)
                      news = news.OrderBy(n =&gt; n.CategoryToString).ToList();
                  else
                      news = news.OrderByDescending(n =&gt; n.CategoryToString).ToList();
  
  
              List&lt;NewsDTO&gt; newsDTO = new List&lt;NewsDTO&gt;();
  
              foreach (var item in news)
              {
                  NewsDTO newsDTOtemp = new NewsDTO();
  
                  newsDTOtemp.BlobName = item.BlobName;
                  newsDTOtemp.DatePublished = item.DatePublished;
                  newsDTOtemp.NewsID = item.NewsID;
                  newsDTOtemp.PreviewText = item.PreviewText;
                  newsDTOtemp.PublishedByEmail = item.PublishedByEmail;
                  newsDTOtemp.PublishedByFullName = item.PublishedByFullName;
                  newsDTOtemp.PublishedByID = item.PublishedByID;
                  newsDTOtemp.Title = item.Title;
                  newsDTOtemp.CategoryID = item.Category.CategoryID;
                  newsDTOtemp.CategoryToString = item.Category.Name;
  
                  newsDTO.Add(newsDTOtemp);
              }
  
              //Pagination
              NewsPagedListDTO newsResultDTO = new NewsPagedListDTO();
              newsResultDTO.NewsDTO = (PagedList&lt;NewsDTO&gt;)newsDTO.ToPagedList(page ?? 1, pageSize);
  
              return newsResultDTO;
          }
  </code></pre>
  
  <p><strong>Pagination in my view:</strong></p>
  
  <pre><code>@Html.PagedListPager(Model.NewsPagedListDTO.NewsDTO, page =&gt; Url.Action("News", new
     {
         page,
         newsTitle = Request.QueryString["NewsTitle"],
         categoryID = Request.QueryString["categoryID"],
         orderByTitle = Request.QueryString["orderByTitle"],
         orderByPublisher = Request.QueryString["orderByPublisher"],
         orderByDate = Request.QueryString["orderByDate"],
         orderByCategory = Request.QueryString["orderByCategory"]
     }),
      new PagedListRenderOptions()
      {
          Display = PagedListDisplayMode.IfNeeded,
          MaximumPageNumbersToDisplay = 5,
          DisplayEllipsesWhenNotShowingAllPageNumbers = false,
          DisplayLinkToPreviousPage = PagedListDisplayMode.Never,
          DisplayLinkToNextPage = PagedListDisplayMode.Never,
          LinkToFirstPageFormat = String.Format("«"),
          LinkToLastPageFormat = String.Format("»")
      })
  </code></pre>
  
  <p><strong>Questions</strong></p>
  
  <ol>
  <li><p>It's the first time I'm using PagedList. What's the point in having postback for changing the page when the full results is retrieved? Isn't it better with client side pagination then? Currently I am retrieving all posts from DB with:</p>
  
  <p>news = (from n in DB.News
  select n).ToList();</p>
  
  <p>And after data is retrieved, sort with parameters..</p>
  
  <p>Sure the result is easy to cache but.. I rather only get data for just one page.</p></li>
  <li><p>How would I only get data for the current page with my optional parameters? I have used stored procedures for this before but I don't think it's possible  with PagedList.</p></li>
  <li><p>How can I have cleaner code for optional parameters in my LINQ query? I don't like all those if statements..</p></li>
  </ol>