title: OData Property count of entire collection
body: <p>I have an asp.net core 3.2 api which uses ef core and OData to return data to the client.</p>
  <p>I need to be able return the count of an expanded one-to-one property in a list of objects along with the parent object list. Since I am paging the results, the <code>@odata.count</code> represents the count of records in the database that match the criteria, not the count of records it actually returns.</p>
  <p>To explain lets say we have a couple of classes</p>
  <pre class="lang-cs prettyprint-override"><code>    public class Order
      {
          public int Id { get; set; }
          public string Name { get; set; }
          public Customer Customer { get; set; }
      }
  
      public class Customer
      {
          public int Id { get; set; }
          public string Name { get; set; }
          public ICollection&lt;Order&gt; Orders { get; set; }
      }
  </code></pre>
  <p>Now I've seen other Stackoverflow Q&amp;A that will return the number of <code>Orders</code> within a <code>Customer</code>.</p>
  <p>But I need to return a list of <code>Orders</code> and while expanding the <code>Customer</code>, I need to know how many times a <code>Customer</code> appears in the list of <code>Orders</code>.</p>
  <p>To provide some context as to why I need this, is because I am building a list filter form like this below. Users can then click the checkbox to filter the list by Customer.</p>
  <p><a href="https://i.stack.imgur.com/Vo9It.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Vo9It.png" alt="Filter criteria" /></a></p>
  <p>Using this query for starters:</p>
  <pre><code>https://localhost:5001/odata/Orders?$count=true&amp;$expand=Customer&amp;$filter=Some criteria&amp;$top=7&amp;$skip=0
  </code></pre>
  <p>So a desired result would be:</p>
  <pre class="lang-json prettyprint-override"><code>{
      &quot;@odata.context&quot;: &quot;https://localhost:5001/odata/$metadata#Orders(Customer())&quot;,
      &quot;@odata.count&quot;: 20, // the count of Orders that match criteria in database
      &quot;value&quot;: [
        {
          &quot;Id&quot;: 1,
          &quot;Name&quot;: &quot;Order 1&quot;,
          &quot;Customer&quot;: {
              &quot;@odata.count&quot;: 6, // this is what i need the count of Customers that are in Orders that match criteria in database
              &quot;Id&quot;: 1,
              &quot;Name&quot;: &quot;Contoso&quot;
          }
        },
        {
          &quot;Id&quot;: 2,
          &quot;Name&quot;: &quot;Order 2&quot;,
          &quot;Customer&quot;: {
              &quot;@odata.count&quot;: 6,
              &quot;Id&quot;: 1,
              &quot;Name&quot;: &quot;Contoso&quot;
          }
        },
        {
          &quot;Id&quot;: 3,
          &quot;Name&quot;: &quot;Order 3&quot;,
          &quot;Customer&quot;: {
              &quot;@odata.count&quot;: 6,
              &quot;Id&quot;: 1,
              &quot;Name&quot;: &quot;Contoso&quot;
          }
        },
        {
          &quot;Id&quot;: 4,
          &quot;Name&quot;: &quot;Order 4&quot;,
          &quot;Customer&quot;: {
              &quot;@odata.count&quot;: 4,
              &quot;Id&quot;: 2,
              &quot;Name&quot;: &quot;Acme&quot;
          }
        },
        {
          &quot;Id&quot;: 5,
          &quot;Name&quot;: &quot;Order 5&quot;,
          &quot;Customer&quot;: {
              &quot;@odata.count&quot;: 4,
              &quot;Id&quot;: 2,
              &quot;Name&quot;: &quot;Acme&quot;
          }
        },
        {
          &quot;Id&quot;: 6,
          &quot;Name&quot;: &quot;Order 6&quot;,
          &quot;Customer&quot;: {
              &quot;@odata.count&quot;: 10,
              &quot;Id&quot;: 3,
              &quot;Name&quot;: &quot;Foo Bar Inc&quot;
          }
        },
        {
          &quot;Id&quot;: 7,
          &quot;Name&quot;: &quot;Order 7&quot;,
          &quot;Customer&quot;: {
              &quot;@odata.count&quot;: 10,
              &quot;Id&quot;: 3,
              &quot;Name&quot;: &quot;Foo Bar Inc&quot;
          }
        }
      ]
    }
    
  </code></pre>