按年和月分组项目清单

时间:2014-02-03 17:59:21

标签: c# javascript json

我有这个时间线课

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; }
}

我将时间轴列表作为json返回。示例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}]}}

现在我想按年份和月份分组。在java脚本或c#这样的任何帮助。

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

}

}

1 个答案:

答案 0 :(得分:1)

您可以使用LINq的GroupBy来做到这一点,但我必须警告您:代码可能看起来有点令人困惑。尝试类似:

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