0

I've this timeline class

public class TimeLine
{
    public DateTime date { get; set; }
    public string UserName { get; set; }
    public string EventName { get; set; }
    public string EventDescription { get; set; }
    public int EventType { get; set; }
}

i'm returning a list of timeline as json. Sample json

{"d":{"Result":"OK","Timeline":[{"date":"\/Date(1388793600000)\/","UserName":"Rahul Aggrawal","EventName":"Appointment","EventDescription":"Appointment scheduled at 01/04/2014 12:00:00 AM 2014-01-04T09:00:00","EventType":1},{"date":"\/Date(1390348800000)\/","UserName":"Rahul Aggrawal","EventName":"Appointment","EventDescription":"Appointment scheduled at 01/22/2014 12:00:00 AM 2014-01-22T10:00:00","EventType":1},{"date":"\/Date(1390694400000)\/","UserName":"Rahul Aggrawal","EventName":"Appointment","EventDescription":"Appointment scheduled at 01/26/2014 12:00:00 AM 2014-01-26T11:00:00","EventType":1},{"date":"\/Date(1391126400000)\/","UserName":"Rahul Aggrawal","EventName":"Appointment","EventDescription":"Appointment scheduled at 01/31/2014 12:00:00 AM 2014-01-31T11:00:00","EventType":1},{"date":"\/Date(1387507436000)\/","UserName":"James Paul","EventName":"Order","EventDescription":"54230","EventType":2},{"date":"\/Date(1387507436000)\/","UserName":"Rahul Aggrawal","EventName":"Order","EventDescription":"54234","EventType":2},{"date":"\/Date(1387507436000)\/","UserName":"Rahul Aggrawal","EventName":"Order","EventDescription":"54237","EventType":2},{"date":"\/Date(1387507436000)\/","UserName":"Rahul Aggrawal","EventName":"Order","EventDescription":"54238","EventType":2},{"date":"\/Date(1387507436000)\/","UserName":"Rahul Aggrawal","EventName":"Order","EventDescription":"54240","EventType":2},{"date":"\/Date(1387507436000)\/","UserName":"Rahul Aggrawal","EventName":"Order","EventDescription":"54265","EventType":2},{"date":"\/Date(1387507436000)\/","UserName":"rahul agrawal","EventName":"Order","EventDescription":"54270","EventType":2},{"date":"\/Date(1387507436000)\/","UserName":"rahul agrawal","EventName":"Order","EventDescription":"54273","EventType":2},{"date":"\/Date(1387507436000)\/","UserName":"tarun sharma","EventName":"Order","EventDescription":"54281","EventType":2},{"date":"\/Date(1387507436000)\/","UserName":"neha gupta","EventName":"Order","EventDescription":"54287","EventType":2},{"date":"\/Date(1387515682000)\/","UserName":"Rahul Agrawal","EventName":"Order","EventDescription":"54909","EventType":2},{"date":"\/Date(1387516768000)\/","UserName":"Rahul Agrawal","EventName":"Order","EventDescription":"54910","EventType":2},{"date":"\/Date(1387873613000)\/","UserName":"Rahul Aggrawal","EventName":"Order","EventDescription":"54916","EventType":2},{"date":"\/Date(1389931752000)\/","UserName":"Rahul Aggrawal","EventName":"Order","EventDescription":"54917","EventType":2},{"date":"\/Date(1389937365000)\/","UserName":"Rahul Aggrawal","EventName":"Order","EventDescription":"54918","EventType":2},{"date":"\/Date(1389938529000)\/","UserName":"Rahul Aggrawal","EventName":"Order","EventDescription":"54919","EventType":2}]}}

Now i want to group it by year and month. Any help either in java script or c# like this.

{
"2012":{
"Feb":[{"date":"Feb 28,2012","sd":"test3","txt":"this is just a test3"}],
"Nov":[{"date":"Nov 11,2012","title":"test2","txt":"this is just a test2"}],
"Dec":[{"date":"Dec 12,2012","title":"test1","txt":"this is just a test"},
    {"date":"Dec 22,2012","title":"test4","txt":"this is just a test4"}]    

},

"2010":{
"Aug":[{"date":"Aug 15,2010","title":"test7","txt":"this is just a test7"},
       {"date":"Aug 21,2010","title":"test5","txt":"this is just a test5"}],
"Nov":[{"date":"Nov 1, 2010","title":"test6","txt":"this is just a test6"}] 

}

}
Siz S
  • 826
  • 3
  • 8
  • 26
  • Is it possible to change the object? I think you'd have an easier time by adding some kind of grouping string/label to the object serverside before sending it to the browser. I'm assuming by grouping, you're talking about for display in the browser. – Tim Coker Feb 03 '14 at 18:03

1 Answers1

1

You can use LINq's GroupBy for doing that, but I must warn you: code might look a bit confusing. Try something like:

var result = yourList.GroupBy(x => x.date.Year) //grouping timelines on their years
    .ToDictionary(year => year.Key, //creating hash indexed by year
        g => g.GroupBy(x => x.date.ToString("MMM")) //grouping values on their months
    .ToDictionary(month => month.Key, //another hash indexed by month
        data => data.Select(tl => new { UserName = tl.UserName, ... }))); //finally got to the desired obj
rla4
  • 1,148
  • 1
  • 13
  • 25