2013-08-18 160 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