2015-04-24 43 views
0

我正在开始使用d3和cal-heatmap,并且有一个与数据相关的问题。来自linq的cal-heatmap数据查询

根据文档(http://kamisama.github.io/cal-heatmap/#data-format)数据需要在一个格式像这样http://kamisama.github.io/cal-heatmap/datas-years.json

我使用提供LinqPad4:

int userId = 4; 
DateTime start = new DateTime(2015, 1 , 2); 
DateTime end = new DateTime(2015, 6, 30); 

var result = (from r in Response 
    where (r.RespondentUserId == userId) 
    && (r.ResponseBegin >= start) 
    && (r.ResponseBegin <= end) 
    group r by new { date = r.ResponseBegin.Date } into grp 
    select new 
    { 
     Ticks = grp.Key.date.Ticks, 
     Reports = grp.Count() 
    }) 
    .ToList() 
    .Select (r => new 
    { 
     timestamp = new TimeSpan(r.Ticks).TotalSeconds, 
     value = r.Reports 
    }); 

    new JavaScriptSerializer().Serialize(result).Dump(); 

产生本格式:

[{ “时间戳”:63556099200, “值”:2},{ “时间戳”:63556185600, “值”:4},{ “时间戳”:63556272000, “值”:2},{ “时间戳”:63556358400, “值”:1},{ “时间戳”:63556704000,“VA略 “:2},{” 时间戳 “:63556790400,” 值 “:1},{” 时间戳 “:63556876800,” 值 “:1},{” 时间戳 “:63556963200,” 值 “:1},{”时间戳 “:63557222400,” 值 “:1},{” 时间戳 “:63557308800,” 值 “:1},{” 时间戳 “:63557827200,” 值 “:1},{” 时间戳 “:63557913600,” 值” :1},{ “时间戳”:635.58亿, “值”:2},{ “时间戳”:63558086400, “值”:1},{ “时间戳”:63558172800, “值”:1},{ “时间戳” :63559296000, “值”:3},{ “时间戳”:63559728000, “值”:2},{ “时间戳”:63559814400, “值”:1},{ “时间戳”:63559900800, “值”:3 },{ “时间戳”:63559987200, “值”:1},{ “时间戳”:63560246400, “值”:2},{ “时间戳”:63560332800, “值”:1},{ “时间戳”:63560419200 “值”:2},{ “时间戳”:63560505600, “值”:2},{ “时间戳”:63560592000, “值”:1},{ “时间戳”:63560937600, “值”:1}, { “时间戳”:63561456000, “值”:4},{ “时间戳”:63561801600, “值”:2},{ “时间戳”:63562060800, “值”:2},{ “时间戳”:63562147200,”值 “:1},{” 时间戳 “:63562233600,” 值 “:2},{” 时间戳 “:63562320000,” 值 “:1},{” 时间戳“:6356240 6400, “值”:1},{ “时间戳”:63562665600, “值”:1},{ “时间戳”:63562752000, “值”:1},{ “时间戳”:63562838400, “值”:2} ,{ “时间戳”:63562924800, “值”:1},{ “时间戳”:63563270400, “值”:2},{ “时间戳”:63563961600, “值”:2},{ “时间戳”:63564048000, “值”:2},{ “时间戳”:63564134400, “值”:3},{ “时间戳”:63564220800, “值”:3},{ “时间戳”:63564566400, “值”:2},{ “时间戳”:63564739200, “值”:2},{ “时间戳”:63564825600, “值”:1},{ “时间戳”:63565084800, “值”:2},{ “时间戳”:63565171200,“值“:1},{”timestamp“:63565257600,”value“:2},{”timestamp“:63565344000,”value“:2},{”timestamp“:63565430400,”value“:3}]

linq查询可以直接生成正确的格式吗?

回答

2

你非常接近。你要做的是创建一个Dictionary,其序列将类似于格式你想:

var result = (from r in Response 
    where (r.RespondentUserId == userId) 
    && (r.ResponseBegin >= start) 
    && (r.ResponseBegin <= end) 
    group r by new { date = r.ResponseBegin.Date } into grp 
    select new 
    { 
     Ticks = grp.Key.date.Ticks, 
     Reports = grp.Count() 
    }) 
    .ToDictionary(r => new TimeSpan(r.Ticks).TotalSeconds, r => r.Reports); 

new JavaScriptSerializer().Serialize(result).Dump(); 
0

最终代码:

var result = (from r in Response 
    where (r.RespondentUserId == userId) 
    && (r.ResponseBegin >= start) 
    && (r.ResponseBegin <= end) 
    group r by new { date = r.ResponseBegin.Date } into grp 
    select new 
    { 
     Ticks = grp.Key.date.Ticks, 
     Reports = grp.Count() 
    }) 
    .ToDictionary(r => new TimeSpan(r.Ticks).TotalSeconds.ToString(), r => r.Reports); 

new JavaScriptSerializer().Serialize(result).Dump();