按日期分组(dd.mm.yyyy)

时间:2013-08-18 19:40:57

标签: c# json linq

我有一个带有datetime-property的对象列表。我很难以下面的形式获得Json字符串:

        [{"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);

结果为[{“Day”:17,“Count”:8},{“Day”:18,“Count”: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);

结果为[{“Day”:“/ Date(1376690400000)/”,“Count”:8},{“Day”:“/ Date(1376776800000)/”,“Count”:10}]

DateTime.ToString(“dd.MM.yyyy”)导致linq错误。

1 个答案:

答案 0 :(得分:1)

我在Linqpad.

进行了快速划分

我制作了一个扩展类,并添加了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
  }
]

希望这有帮助。