来自linq查询的cal热图数据

来自linq查询的cal热图数据,linq,cal-heatmap,Linq,Cal Heatmap,我刚开始学习d3和cal热图,有一个与数据相关的问题 根据文档()的要求,需要以如下格式提供数据 我正在使用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) &

我刚开始学习d3和cal热图,有一个与数据相关的问题

根据文档()的要求,需要以如下格式提供数据

我正在使用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();
intuserid=4;
日期时间开始=新的日期时间(2015,1,2);
DateTime end=新的日期时间(2015,6,30);
var结果=(来自响应中的r
其中(r.RespondentUserId==userId)
&&(r.ResponseBegin>=开始)
&&(r.ResponseBegin new)
{ 
时间戳=新的时间跨度(r.Ticks)。总秒数,
值=r.报告
});
新的JavaScriptSerializer().Serialize(result).Dump();
产生这种格式的:

[{“timestamp”:63556099200,“value”:2},{“timestamp”:63556185600,“value”:4},{“timestamp”:63556272000,“value”:2},{“timestamp”:63556358400,“value”:1},{“timestamp”:63556704000,“value”:2},{“timestamp”:63556790400,“value”:1},{“timestamp”:63556876800,“value”:1},{“timestamp”:635569636963200,“value”:1},{“timestamp”:63557222400,“timestamp”{:63557308800,“value”:1},{“timestamp”:63557827200,“value”:1},{“timestamp”:63557913600,“value”:1},{“timestamp”:6355800000,“value”:2},{“timestamp”:63558086400,“value”:1},{“timestamp”:63558172800,“value”:1},{“timestamp”:63559296000,“value”:3},{“timestamp”:63559728000,“value”:2},{“timestamp”:63559814400,“value”:63550803,{“timestamp”:63559900,{{“timestamp”:63559987200,“value”:1},{“timestamp”:63560246400,“value”:2},{“timestamp”:63560332800,“value”:1},{“timestamp”:63560419200,“value”:2},{“timestamp”:635605056000,“value”:2},{“timestamp”:63560592000,“value”:1},{“timestamp”:63560937600,“value”:1},{“timestamp”:63561456000,“value”:4},{“timestamp”:63561801600,“value”:635660800,{value:2},{“timestamp”:63562147200,“value”:1},{“timestamp”:63562233600,“value”:2},{“timestamp”:63562320000,“value”:1},{“timestamp”:63562406400,“value”:1},{“timestamp”:63562665600,“value”:1},{“timestamp”:63562752000,“value”:1},{“timestamp”:63562838400,“value”:2},{“timestamp”:63562924800,“value”:1},{“timestamp”:63563270400,“value”:timestamp,{“:63563961600,“值”:2},{“时间戳”:63564048000,“值”:2},{“时间戳”:63564134400,“值”:3},{“时间戳”:63564220800,“值”:3},{“时间戳”:63564566400,“值”:2},{“时间戳”:63564739200,“值”:2},{“时间戳”:63564825600,“值”:1},{“时间戳”:63565084800,“值”:2},{“时间戳”:635651200,“值”:635671200,“时间戳”:5250},{,{“时间戳”:63565344000,“值”:2},{“时间戳”:63565430400,“值”:3}]


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

您已经非常接近了。您要做的是创建一个
字典,它序列化后将看起来像您想要的格式:

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();
var result=(来自响应中的r
其中(r.RespondentUserId==userId)
&&(r.ResponseBegin>=开始)
&&(r.responsebeginnewtimespan(r.Ticks).TotalSeconds,r=>r.Reports);
新的JavaScriptSerializer().Serialize(result).Dump();
最终代码:

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();
var result=(来自响应中的r
其中(r.RespondentUserId==userId)
&&(r.ResponseBegin>=开始)
&&(r.responsebeginnewtimespan(r.Ticks).TotalSeconds.ToString(),r=>r.Reports);
新的JavaScriptSerializer().Serialize(result).Dump();