title: Adding custom paging to OData(v4) Web API C#
body: <p>I am wondering would anyone be able to help me. I am trying implement custom paging into a OData feed (oData v4). I am filling the IEnumerable from a stored procedure to which I have added a means of paging by below (Working Perfect)</p>
  
  <pre><code>SELECT  *
  FROM  ( 
      SELECT    ROW_NUMBER() OVER ( ORDER BY OrderID ) AS RowNum, *
      FROM      dbo.Order
      WHERE     CompanyID = @CompanyID
  ) AS RowConstrainedResult
  WHERE   RowNum &gt;= @Start AND RowNum &lt;= @Finish
  ORDER BY RowNum
  </code></pre>
  
  <p>However the issue I am having is that the below code works perfect apart from the paging is not working</p>
  
  <pre><code>[EnableQuery]
  public async Task&lt;PageResult&lt;Order&gt;&gt; GetOrders(ODataQueryOptions&lt;Order&gt; queryOptions)
  {
      int CompanyID = User.Identity.GetCompanyID().TryParseInt(0);
  
      ODataQuerySettings settings = new ODataQuerySettings()
      {
          PageSize = 100,
      };
  
      int OrderCount = _OrderRepo.GetOrderCount(CompanyID);
      int Skip = 0;
      if (queryOptions.Skip != null)
      {
          Skip =  queryOptions.Skip.Value;
      }
  
      IEnumerable&lt;Order&gt; results = await _OrderRepo.GetAll(CompanyID, Skip, 100);
  
      IQueryable result = queryOptions.ApplyTo(results.AsQueryable(), settings);
  
      Uri uri = Request.ODataProperties().NextLink;
      Request.ODataProperties().TotalCount =  OrderCount;
  
      PageResult&lt;Order&gt; response = new PageResult&lt;Order&gt;(
      result as IEnumerable&lt;Order&gt;,
      uri, Request.ODataProperties().TotalCount);
  
      return response;
  }
  </code></pre>
  
  <p>Basically I am trying to page through the results in batches of 100, taking 100 at time from the database only as needed.   </p>
  
  <p>When I first call that controller I get the expected results.</p>
  
  <pre><code>http://localhost:24600/Data/Orders
  </code></pre>
  
  <p>But when I query this nextLink (below), I get no errors but no results despite the fact there is over 50,000 results in the database and the IEnumerable results has been updated correctly.</p>
  
  <pre><code>http://localhost:24600/Data/Orders?$skip=100
  </code></pre>
  
  <p>I would be extremely gratefully if someone help me out with this as I am new to using odata.</p>
  
  <p>Update</p>
  
  <p>It looks like, it is counting the size of the first batch and taking that as the total size, even though I am setting the total size. It seems to not be able to go above the initial size.</p>