I wonder if anyone can help.
I am using the following code to retrieve some data from a database and output it as JSON:
public ActionResult PieDataPropertyTotalsByStatusSO()
{
var db = new Coyote_AcquisitionsContext();
IQueryable<Property> query = db.Properties;
var propertyTotalsByStatus =
query.GroupBy(p => p.Status).OrderByDescending(p => p.Count()).Select(pg => new { Status = pg.FirstOrDefault().Status.Name, Count = pg.Count() }).ToList();
return Json(propertyTotalsByStatus, JsonRequestBehavior.AllowGet);
}
This outputs data as follows:
[
{ "Count" : 1205,
"Status" : "General Stock (Rejected)"
},
{ "Count" : 816,
"Status" : "New Introduction"
},
{ "Count" : 653,
"Status" : "Potential"
},
{ "Count" : 110,
"Status" : "Completed"
}
]
However to save bandwidth (and because this is the format the charting library I am using expects) I need to strip out the keys and just end up with:
[
[ 1205, "General Stock (Rejected)" ],
[ 816, "New Introduction" ],
[ 653, "Potential" ],
[ 110, "Completed" ]
]
I can't find an elegant way of doing it. I may end up with more or less fields than just Count and Status so I can't use a solution that iterates through on the basis of their being exactly two different fields.
Any help massively appreciated.
EDIT:
Corrected the JSON that I am looking to output.
[[1205,"General Stock (Rejected)"],...]. Is that a valid option?