2015-04-24 41 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();