title: ASP.NET Core WebApi Retrieve Last n Records
body: <p>I have put together a <code>GET</code> request that returns the last <code>n</code> (5) records on WebApi. Simply creating a call to get the entire list back async, then count the last five items, add them to a List of the custom type, that is <code>Application</code> in this case, then add then to a new list and return that.</p>
  <p>Is there a more efficient way to run this query using Entity Framework core on .Net Core 6 webapi?</p>
  <p>Here is my current code:</p>
  <pre class="lang-cs prettyprint-override"><code>// GET: api/Application/last5
  [HttpGet(&quot;last5&quot;)]
  public async Task&lt;ActionResult&lt;IEnumerable&lt;Application&gt;&gt;&gt; Get5Applications()
  {
      var list = await _context.Applications.ToListAsync();
  
      List&lt;Application&gt; returnedData = new List&lt;Application&gt;();
  
      for (int i = Math.Max(0, list.Count - 5); i &lt; list.Count; ++i)
      {
          returnedData.Add(list[i]);
      }
  
      return returnedData;
  }
  </code></pre>