I have put together a GET
request that returns the last n
(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 Application
in this case, then add then to a new list and return that.
Is there a more efficient way to run this query using Entity Framework core on .Net Core 6 webapi?
Here is my current code:
// GET: api/Application/last5
[HttpGet("last5")]
public async Task<ActionResult<IEnumerable<Application>>> Get5Applications()
{
var list = await _context.Applications.ToListAsync();
List<Application> returnedData = new List<Application>();
for (int i = Math.Max(0, list.Count - 5); i < list.Count; ++i)
{
returnedData.Add(list[i]);
}
return returnedData;
}