c# - Group by date-part (dd.mm.yyyy) -


i've got list of objects datetime-property. struggle in getting json-string in following form of it:

        [{"date":17.08.2013,"count":8},{"day":18.08.2013,"count":10}] 

this

        var results = in db.stalkings                       group new { d = a.begin.day }                       g                       select new { day = g.key.d, count = g.count() };         return json( results, jsonrequestbehavior.allowget); 

results in [{"day":17,"count":8},{"day":18,"count":10}]. , this

        var results1 = in db.stalkings                        group entityfunctions.truncatetime(a.begin)                        g                        select new { day = g.key, count = g.count() };          return json( results, jsonrequestbehavior.allowget); 

results in [{"day":"/date(1376690400000)/","count":8},{"day":"/date(1376776800000)/","count":10}]

datetime.tostring("dd.mm.yyyy") leads linq error.

i did quick scratchup in linqpad.

i made extension class , added extension methods provided here. cannot use dumpjson() outside of linqpad, visualisation of data.

for sake of simplicity used list of datetime values. here code wil provide following output:

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 string contains json object, round-trip serialization format it:               var stringvalue = value 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;        }  } 

output:

[   {     "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   } ] 

hope helps.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo