Click here to Skip to main content
13,737,154 members
Rate this:
 
Please Sign up or sign in to vote.
I have the following table:

<pre lang="c++">Cans
 
Id: int 
HospitalId: (Foreign Key) int 
operDt: DateTime


I need to calculate the total number of cans rows grouped by hospitalId

to get a similar result without Id

screenshot[^]

What I have tried:

The repository :

public Task GetCansMin(DateTime startdt, DateTime enddt)
            {
                using (appContext)
                {
                    return appContext.Cans.Where(c => c.OperDt > startdt && c.OperDt < enddt).GroupBy(c => c.HospitalId)
                          .Select(g => new
                          {
                              NumberofCancelled = g.Count() 
                          }).ToArrayAsync();
                         
                };
            }


The controller:



[HttpGet("bydate")]
            public IActionResult GetCanMin(DateTime startdt, DateTime endd)
            {
                var res = this._unitOfWork.Cans.GetCansMin(startdt, endd);
                return Ok(res);
            }


When passing the request with postman:

http://localhost:56964/api/cansdatas/bydate?startdt=2016-01-01&enddt=2016-12-31


instead of receiving an array of results I get an empty object....
Could you help?
Posted 4-Nov-17 7:15am
Updated 6-Nov-17 7:06am
v3

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180920.1 | Last Updated 6 Nov 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100