Help with Jagged Array needed, getting IQueryable results into an array
Does anyone know how I can get my results currently in qry IQueryable object into a jagged array in the format:
series:
[{
name: '2',
data: [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
}, {
name: '3',
data: [1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 3, 0]
}]
My problem is the code I have at the moment wraps double quotes around the 12 element data part, i.e. it looks like this in the debugger on client side:
?result[0] {...} [0]: "2" [1]: "[0,0,0,0,0,0,1,0,0,0,0,0]" ?result[1] {...} [0]: "3" [1]: "[1,0,0,0,0,0,0,0,0,0,0,0]"
The problem is my array contains a single string element rather than an array of 12 numbers.
Here is my controller code that at the moment returns the 12 number part as one big string :
var qry = from i in _db.Complaints
where i.Site.SiteDescription.Contains(searchTextSite)
&& (i.Raised >= startDate && i.Raised <= endDate)
group i by i.ComplaintNatureTypeId.ToString()
into grp select new
{
Type = grp.Key,
Count = "[" + grp.Where(c => c.Raised.Month == 1).Count() + "," +
grp.Where(c => c.Raised.Month == 2).Count() + "," +
grp.Where(c => c.Raised.Month == 3).Count() + "," +
开发者_JS百科 grp.Where(c => c.Raised.Month == 4).Count() + "," +
grp.Where(c => c.Raised.Month == 5).Count() + "," +
grp.Where(c => c.Raised.Month == 6).Count() + "," +
grp.Where(c => c.Raised.Month == 7).Count() + "," +
grp.Where(c => c.Raised.Month == 8).Count() + "," +
grp.Where(c => c.Raised.Month == 9).Count() + "," +
grp.Where(c => c.Raised.Month == 10).Count() + "," +
grp.Where(c => c.Raised.Month == 11).Count() + "," +
grp.Where(c => c.Raised.Month == 12).Count() + "]"
};
return Json(qry.ToArray(), JsonRequestBehavior.AllowGet);
You need to pass an object and Json
will serialize it to a JSON string.
Count = new int[] {
grp.Where(c => c.Raised.Month == 1).Count(),
grp.Where(c => c.Raised.Month == 2).Count(),
grp.Where(c => c.Raised.Month == 3).Count(),
grp.Where(c => c.Raised.Month == 4).Count(),
grp.Where(c => c.Raised.Month == 5).Count(),
grp.Where(c => c.Raised.Month == 6).Count(),
grp.Where(c => c.Raised.Month == 7).Count(),
grp.Where(c => c.Raised.Month == 8).Count(),
grp.Where(c => c.Raised.Month == 9).Count(),
grp.Where(c => c.Raised.Month == 10).Count(),
grp.Where(c => c.Raised.Month == 11).Count(),
grp.Where(c => c.Raised.Month == 12).Count()
}
Or you could do something like:
Count = Enumerable.Range(1, 12).Select(x => grp.Where(c => c.Raised.Month == x).Count())
You are creating the count property as a string, should it not be an array:
Count = new [] {
grp.Where(c => c.Raised.Month == 1).Count(),
grp.Where(c => c.Raised.Month == 2).Count(),
...
grp.Where(c => c.Raised.Month == 12).Count()
}
精彩评论