首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >来自linq查询的cal-heatmap数据

来自linq查询的cal-heatmap数据
EN

Stack Overflow用户
提问于 2015-04-24 19:12:04
回答 2查看 100关注 0票数 0

我开始学习d3和,并且有一个与数据相关的问题。

根据文档(http://kamisama.github.io/cal-heatmap/#data-format),需要以类似于http://kamisama.github.io/cal-heatmap/datas-years.json的格式提供数据

我正在使用LinqPad4:

代码语言:javascript
运行
复制
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,“值”:2},{“时间戳”:63556790400,“值”:1},{“时间戳”:63556876800,“值”:1},{“时间戳”:63556963200,“值”:1},{“时间戳”:63557222400,“值”:1},{“时间戳”:63557308800,“值”:1},{“时间戳”:63557827200,“值”:1},{“时间戳”:63557913600,“值”:1},{“时间戳”:63558000000,“值”: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},{“时间戳”:63562406400,“值”: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},{“时间戳”:63565257600,“值”:2}。{“时间戳”:63565344000,“值”:2},{“时间戳”:63565430400,“值”:3}

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-24 19:40:46

你当时很亲密。您想要做的是创建一个Dictionary,序列化后的格式将类似于您想要的格式:

代码语言:javascript
运行
复制
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();
票数 2
EN

Stack Overflow用户

发布于 2015-04-25 02:52:23

最终代码:

代码语言:javascript
运行
复制
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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29855629

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档