Home > Mobile >  How to show the total number of requests from each object
How to show the total number of requests from each object

Time:02-19

I have the list of requests from offices.

Model:

public class Request
{
   public int Id { get; set; }
   public DateTime Created { get; set; }
   public string Office { get; set; } = "";
   public string Status { get; set; } = "";
}

Controller:

public IActionResult Requests(int id)
{
    var requests = _repo.GetAllRequests();   
    return View(requests);
}

Repository:

public List<Request> GetAllRequests()
{
    return _ctx.Requests.ToList();
}

View:

@model IEnumerable<Request>
<table>
    <thead>
        <tr>
            <th>Date</th>
            <th>Status</th>
            <th>Office</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var request in Model)
        {
            <tr>
                <th>@request.Created.ToString()</th>
                <th>@request.Status</th>
                <th>@request.Office</th>
            </tr>
        }
    </tbody>
</table>

I need a function that will count the number of requests from each office. For instance, I have the table:

date-status-office:
18feb-opened-101
18feb-opened-102
18feb-closed-103
18feb-opened-101
18feb-opened-101
18feb-closed-103

The result should be:

101: 3 requests
103: 2 requests
102: 1 request

Just in case, this is a graphical explanation: image of desired output

How do I show the total requests per office?

CodePudding user response:

You could use GroupBy() to group the requests by Office and take the count of each group. In the controller you could do something like this which returns an anonymous object:

var requests = _repo.GetAllRequests();
var groupedRequests = requests.GroupBy(r => r.Office)
                              .Select(g => new { Office = g.Key, Count = g.Count() });

In the view it would be something like:

@{
    var groupedRequests = Model.GroupBy(r => r.Office)
                               .Select(g => new { Office = g.Key, Count = g.Count() }); 
}
...
// display however you want
@foreach (var request in groupedRequests)
{
    <div>
        @request.Office: @request.Count requests
    </div>
}

However, consider creating a view model to hold your Request data:

public class ViewModel
{
    public List<Request> Requests { get; set; }
    public List<RequestSummary> RequestSummaries { get; set; }
}

public class RequestSummary
{
    public string Office { get; set; }
    public int Count { get; set; }
}

Then in the controller you'd create a RequestSummary instead of an anonymous object:

public IActionResult Requests(int id)
{
    var requests = _repo.GetAllRequests(); 
    var summaries = requests.GroupBy(r => r.Office)
                            .Select(g => new RequestSummary 
                            { 
                                Office = g.Key, 
                                Count = g.Count() 
                            })
                            .ToList();

    var viewModel = new ViewModel();
    viewModel.Requests = requests;
    viewModel.RequestSummaries = summaries;

    return View(viewModel);
}

Update the model reference in view and display data using Model.Requests and Model.RequestSummaries:

@model ViewModel
...
  • Related