You're returning a
Task
, rather than the results of that
Task
.
You're also disposing of the
DbContext
before the
Task
has completed.
(There's also a missing "t" in the action parameter name, but I'm not sure whether that's just a typo in your question.)
Try something like this:
public async Task<object> GetCansMin(DateTime startdt, DateTime enddt)
{
using (appContext)
{
return await appContext.Cans
.Where(c => c.OperDt > startdt && c.OperDt < enddt)
.GroupBy(c => c.HospitalId)
.Select(g => new
{
NumberofCancelled = g.Count()
})
.ToArrayAsync();
}
}
[HttpGet("bydate")]
public async Task<IActionResult> GetCanMin(DateTime startdt, DateTime enddt)
{
var res = await this._unitOfWork.Cans.GetCansMin(startdt, enddt);
return Ok(res);
}
NB: Since you're storing the
DbContext
in a field, I'd be inclined to remove the
using (appContext)
blocks from your methods, and have your unit-of-work class implement
IDisposable
instead. You can then register that class to be disposed after each request:
Four ways to dispose IDisposables in ASP.NET Core[
^]