2013-08-18 208 views
2

我有一個日期時間屬性的對象列表。按日期部分組(dd.mm.yyyy)

 [{"Date":17.08.2013,"Count":8},{"Day":18.08.2013,"Count":10}] 

 var results = from a in db.Stalkings 
         group a by new { d = a.Begin.Day } 
         into g 
         select new { Day = g.Key.d, Count = g.Count() }; 
     return Json(results, JsonRequestBehavior.AllowGet); 

結果[{ 「日」::17, 「計數」:8},{「日我在它的下面的形式得到的JSON串鬥爭「:18,」 計數「:10}]。這

 var results1 = from a in db.Stalkings 
         group a by EntityFunctions.TruncateTime(a.Begin) 
         into g 
         select new { Day = g.Key, Count = g.Count() }; 
     return Json(results, JsonRequestBehavior.AllowGet); 

結果[{ 「日」: 「/日期(1376690400000)/」, 「計數」:8},{ 「日」: 「/日期(1376776800000)/」, 「計數」 :10}]

DateTime.ToString(「dd.MM.yyyy」)導致linq錯誤。

+0

這是無效的JSON。 – leppie

+0

提示:使用'DateTime.ToString' – leppie

+0

你想按日期或日期分組嗎? –

回答

1

我的確在Linqpad.

快速scratchup我做了一個擴展類,並添加提供here擴展方法。您不能在Linqpad之外使用DumpJson(),但它只是數據的可視化。

爲了簡單起見,我只使用了DateTime值的列表。這裏是西港島線提供以下輸出代碼:

void Main() 
{ 
    var FooList = new List<DateTime>(); 

    FooList.Add(DateTime.Parse("01.01.2012")); 
    FooList.Add(DateTime.Parse("01.01.2012")); 
    FooList.Add(DateTime.Parse("01.01.2012")); 
    FooList.Add(DateTime.Parse("03.03.2012")); 
    FooList.Add(DateTime.Parse("04.04.2012")); 
    FooList.Add(DateTime.Parse("04.04.2012")); 
    FooList.Add(DateTime.Parse("04.04.2012")); 
    FooList.Add(DateTime.Parse("04.04.2012")); 
    FooList.Add(DateTime.Parse("05.05.2012")); 
    FooList.Add(DateTime.Parse("05.05.2012")); 


    var result = FooList.GroupBy(foo => foo.Date) 
         .Select(res => new 
          { 
           date = res.Key.DateToString("dd.MM.yyyy"), 
           Count = res.Count() 
          }) ; 
    result.DumpJson(); 
} 

public static class MyExtensions 
{ 
    public static object DumpJson(this object value, string description = null) 
     { 
       return GetJsonDumpTarget(value).Dump(description); 
     }  

     public static object DumpJson(this object value, string description, int depth) 
     { 
       return GetJsonDumpTarget(value).Dump(description, depth); 
     }  

     public static object DumpJson(this object value, string description, bool toDataGrid) 
     { 
       return GetJsonDumpTarget(value).Dump(description, toDataGrid); 
     }  

     private static object GetJsonDumpTarget(object value) 
     { 
       object dumpTarget = value; 
       //if this is a string that contains a JSON object, do a round-trip serialization to format it: 
       var stringValue = value as string; 
       if (stringValue != null) 
       { 
        if (stringValue.Trim().StartsWith("{")) 
        { 
          var obj = JsonConvert.DeserializeObject(stringValue); 
          dumpTarget = JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented); 
        } 
        else 
        { 
          dumpTarget = stringValue; 
        } 
       } 
       else 
       { 
        dumpTarget = JsonConvert.SerializeObject(value, Newtonsoft.Json.Formatting.Indented); 
       } 
       return dumpTarget; 
     } 

} 

輸出:

[ 
    { 
    "date": "2012-01-01T00:00:00", 
    "Count": 3 
    }, 
    { 
    "date": "2012-03-03T00:00:00", 
    "Count": 1 
    }, 
    { 
    "date": "2012-04-04T00:00:00", 
    "Count": 4 
    }, 
    { 
    "date": "2012-05-05T00:00:00", 
    "Count": 2 
    } 
] 

希望這有助於。

+0

非常感謝您的努力,看起來這是我儘管我覺得很奇怪,似乎沒有更簡單/更短的方法。 – peter

+0

我需要使用這些擴展方法,因爲Linqpad沒有開箱即可將結果解析爲JSON。我的建議是返回它們,然後在查詢後使用序列化將它們解析爲Json。我認爲問題在於你的分組聲明。 在第一個中,您獲得當天的月份,並在第二個中獲得刻度(?) 請嘗試使用我的分組。它應該爲你做詭計 – Marco