From what I understand based on your question (Please correct me if I'm wrong), looks like you are trying to fire the API with each Npi record for getting the (grouping) count.
In my perspective, it is NOT EFFICIENT, imagine there are 100 records and you will call the API 100 times. This will lead to pulling down your server performance (Handling the same task 100 times) and also database server (Query the same data 100 times).
Instead,
You need to do the data transformation in Controller first by .GroupBy() the npi record and .Count() for each grouped record.
Expected output:
[
{ "npi": 1, "count": 2 },
{ "npi": 2, "count": 1 }
]
With 1 single API call saves you the performance from calling m time as discussed.
What's you need to do:
- Model - Design your desired output with the model class.
- Controller - Perform data transformation: Grouping by
npiId.
- View - Bind the value from Controller.
Model
public class GroupedNpiModel
{
public int NpiId { get; set; }
public int Count { get; set; }
}
Controller
public ActionResult Index()
{
var data = new []
{
new { NpiId = 1, Patientname = "a" } ,
new { NpiId = 1, Patientname = "b" } ,
new { NpiId = 2, Patientname = "c" }
};
var groupedNpi = data.GroupBy(x => x.NpiId)
.Select(g => new GroupedNpiModel
{
NpiId = g.Key,
Count = g.Count()
})
.ToList();
return View(groupedNpi);
}
View
@model IEnumerable<HelloWorldMvcApp.GroupedNpiModel>
@foreach (var npi in Model)
{
<div>
@npi.NpiId -- @npi.Count
</div>
}
Sample program on .NET Fiddle
.GroupBy()in the controller first and return the grouped result to View.