title: Items count in OData v4 WebAPI response
body: <p>How to return number of items in OData v4 HTTP response?</p>
  
  <p>I need this number to pagination, so it should be number of items after filtering, but before 'skip' and 'top'.</p>
  
  <p>I already tried passing '$inlinecount=allpages' and '$count=true' parameters in query options in url (<a href="https://damienbod.wordpress.com/2014/06/13/web-api-and-odata-v4-queries-functions-and-attribute-routing-part-2/" rel="noreferrer">https://damienbod.wordpress.com/2014/06/13/web-api-and-odata-v4-queries-functions-and-attribute-routing-part-2/</a> - "Example of $count"), but my responses from WebAPI always have only query results (collection) - whole response looks like: </p>
  
  <pre><code>[
      {
          "Name":"name1", 
          "age":5
      }, 
      {
          "Name":"name2", 
          "age":15
      }
  ]
  </code></pre>
  
  <p>There is nothing like "odata.count" in the response.</p>
  
  <p>I also tried returning PageResult instead of IQueryable in my WebAPI controller action (like described here: <a href="http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options#server-paging" rel="noreferrer">http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options#server-paging</a>), but Request.GetInlineCount() is deprecated and its value is always null.</p>
  
  <p>Any ideas?</p>
  
  <p>[Update] I just found the same problem here: <a href="https://stackoverflow.com/questions/22854312/webapi-with-odata-nextpage-and-count-not-appearing-in-the-json-response">WebApi with Odata NextPage and Count not appearing in the JSON response</a> and I removed [EnableQuery] attribute and now my response looks like: </p>
  
  <pre><code>{
      "Items":
      [
          {
              "Name":"name1", 
              "age":5
          }, 
          {
              "Name":"name2", 
              "age":15
          }
      ],
      "NextPageLink":null,
      "Count":null
  }
  </code></pre>
  
  <p>But still "Count" is always null. :(</p>
  
  <p><hr>
  Edit: After debugging and searching for count value in Request properties in my controller, I found out that correct Count value is in property named "System.Web.OData.TotalCount". So right now I exctract this value from that request property and my controller looks like that:</p>
  
  <pre><code>public PageResult&lt;People&gt; Get(ODataQueryOptions&lt;People&gt; queryOptions)
  {
      var query = _context.People.OrderBy(x =&gt; x.SomeProperty);
      var queryResults = (IQueryable&lt;People&gt;)queryOptions.ApplyTo(query);
      long cnt = 0;
      if (queryOptions.Count != null)
          cnt = long.Parse(Request.Properties["System.Web.OData.TotalCount"].ToString());
  
      return new PageResult&lt;People&gt;(queryResults, null, cnt);
  }
  </code></pre>
  
  <p>And it works fine, but I still don't know why I have to use workarounds like that.</p>