My intention is to use LINQ query then return JSON for MVC controller.
format I want to achieve:
[{
"A" : {"Count": 2 },
"B" : {"Count": 7 },
}]
or
[{
"A" : [{"Count": 2 }],
"B" : [{"Count": 7 }],
}]
But so far I only can make it like this:
[{
{"MG":"A", "Count": 2 },
{"MG":"B", "Count": 7 }
}]
And I try something like below, it get error
Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.
public JsonResult groupByTable()
{
IQueryable<tblSAPMessage> v = sapMessages();
var data = v.GroupBy(g => g.MaterialGroup)
.Select(g => new { g.Key.ToString() = new {
Count = g.Count()
}});
return Json(data, JsonRequestBehavior.AllowGet);
}
Appreciate if someone can point me to correct direction. Thanks !