繁体   English   中英

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

[英]Group by date-part (dd.mm.yyyy)

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

我在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
  }
]

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM