需要帮助Jagged Array,将IQueryable结果放入数组中

时间:2011-07-26 08:28:16

标签: asp.net-mvc

有谁知道如何将我当前在qry IQueryable对象中的结果转换为以下格式的锯齿状数组:

        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]

                }]

我的问题是我现在的代码在12元素数据部分周围包含双引号,即它在客户端的调试器中看起来像这样:

?结果[0] {...}     [0]:“2”     [1]:“[0,0,0,0,0,0,1,0,0,0,0,0]” ?导致[1] {...}     [0]:“3”     [1]:“[1,0,0,0,0,0,0,0,0,0,0,0]”

问题是我的数组包含一个字符串元素而不是一个包含12个数字的数组。

这是我的控制器代码,目前将12号码部分作为一个大字符串返回:

        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() + "," + 
                        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);

2 个答案:

答案 0 :(得分:1)

您需要传递一个对象,Json会将其序列化为JSON字符串。

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()
}

或者你可以这样做:

Count = Enumerable.Range(1, 12).Select(x => grp.Where(c => c.Raised.Month == x).Count())

答案 1 :(得分:0)

您正在将count属性创建为字符串,如果它不是数组:

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()
}