2

我有一个带有日期时间属性的对象列表。我很难以以下形式获取 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 错误。

4

1 回答 1

1

我在Linqpad 做了一个快速的总结。

我做了一个扩展类并添加了这里提供的扩展方法。您不能在 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
  }
]

希望这可以帮助。

于 2013-08-18T20:56:31.373 回答