按日期分组事件列表 - 然后是时间 - 使用LINQ

时间:2013-09-27 09:50:14

标签: c# asp.net linq

我有一个事件列表,我希望按日期分组,但随后也会按时间分组,因为某些事件共享相同的日期。例如,如果我有以下事件:

Event 1 - 10/23/2013 2:30:00 PM
Event 2 - 10/24/2013 2:30:00 PM
Event 3 - 10/25/2013 2:30:00 PM
Event 3 - 10/25/2013 6:30:00 PM

活动3在同一天有两次放映,但是在不同的时间。理想情况下,我想迭代一个新列表,结果如下:

10/23/2013               10/24/2013               10/25/2013
Name: Event 1            Name: Event 2            Name: Event 3
Time: 2:30 PM            Time: 2:30 PM            Time: 2:30 PM & 6:30 PM

我一直在尝试制作一个新列表,按时间对事件进行分组并制作一个时间子列表。到目前为止,这是我的一些代码,但我收到错误'CS1001:Identifier expected'

List<dynamic> Events = new List<dynamic>();
var grouped = from e in Events
    group e by e.Starts into g
                select new
                {
                    Name = g.First().Name,
                    Starts = g.First().Starts,
                    StartTimes = (from g.First().Starts
                        group d by g.Starts.ToString("hh:mm")
                    )
                };

是否了解如何达到预期效果?

2 个答案:

答案 0 :(得分:1)

我将如何做到这一点:

var grouped =
    from e in events
    group e by e.Starts.Date into g
    let first = g.First()
    select new
    {
        Date = g.Key,
        Events = from e in g
                group e by e.Name into g2
                select new
                {
                    Name = g2.Key,
                    Time = string.Join(", ", g2.Select(e2 => e2.Starts.ToString("t")))
                }
    };

foreach (var day in grouped)
{
    Console.WriteLine ("{0:d}", day.Date);
    foreach (var e in day.Events)
    {
        Console.WriteLine (e.Name);
        Console.WriteLine ("Time: " + e.Time);
    }
    Console.WriteLine ();
}

答案 1 :(得分:1)

您可以使用嵌套的GroupBy

查询:

var groups = events
.GroupBy(ev => ev.Starts.Date)
.Select(dateGroup => new
{
    DateGroup = new { Date = dateGroup.Key, Events = dateGroup.ToList() },
    ListOfTimeGroup = dateGroup
        .GroupBy(dg => dg.Starts.TimeOfDay)
        .ToList()
}).ToList();

foreach (var group in groups)
{
    DateTime date = group.DateGroup.Date;
    List<Event> dateEvents = group.DateGroup.Events;
    Console.WriteLine(string.Join(",", dateEvents));
    foreach( var timeGroup in group.ListOfTimeGroup)
    {
        TimeSpan timeOfday = timeGroup.Key;
        List<Event> timeEventList = timeGroup.ToList();
        Console.WriteLine(string.Join(",", timeEventList));
    }
}

示例数据:

var events = new List<Event>(){
    new Event{Name="Event 1", Starts=new DateTime(2013,10,23, 14, 30, 00)},
    new Event{Name="Event 2", Starts=new DateTime(2013,10,24, 14, 30, 00)},
    new Event{Name="Event 3", Starts=new DateTime(2013,10,25, 14, 30, 00)},
    new Event{Name="Event 4", Starts=new DateTime(2013,10,25, 18, 30, 00)}

};

使用的课程:

public class Event
{
    public string Name { get; set; }
    public DateTime Starts { get; set; }
    public override string ToString()
    {
        return Name + " " + Starts.ToString();
    }
}

输出:

Event 1 23.10.2013 14:30:00
   Event 1 23.10.2013 14:30:00
Event 2 24.10.2013 14:30:00
   Event 2 24.10.2013 14:30:00
Event 3 25.10.2013 14:30:00, Event 4 25.10.2013 18:30:00
   Event 3 25.10.2013 14:30:00
   Event 4 25.10.2013 18:30:00