title: LINQ: latest sum of data within data range
body: <p>I have a snapshot table in my database that holds historical data of appointment availability.</p>
  
  <p>I'm trying to write a LINQ query to get the total number of AvailableSlots within a given date range from the latest snapshot.</p>
  
  <p>Here is what my table looks like:</p>
  
  <p><a href="https://i.stack.imgur.com/LJIZC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LJIZC.png" alt="enter image description here"></a></p>
  
  <p>So with this given data, I'd want my LINQ query to return the sum of AvailableSlots within the date range of 2018-01-01 - 2018-01-02 and with the latest SnapShotTime. So, I'd expect the query to return 4.</p>
  
  <p>Here is what I have so far. </p>
  
  <pre><code>var test = db.snapshots
        .GroupBy(g =&gt;
           g.AppointmentTime &gt;= startDate &amp;&amp;
           g.AppointmentTime &lt;= endDate
        ).Select(s =&gt; s.OrderByDesending(x =&gt; x.SnapShotTime).FirstOrDefault();
  </code></pre>
  
  <p>However, I'm not sure how to put the Sum of available slots into this LINQ query. Any help on writing this query would be appreciated!</p>