0

我有一个事件列表,我想按日期分组,然后按时间分组,因为有些事件共享相同的日期。例如,如果我有以下事件:

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:预期标识符”

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

是否知道如何实现预期结果?

4

2 回答 2

2

您可以使用嵌套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
于 2013-09-27T10:03:05.977 回答
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 ();
}
于 2013-09-27T10:01:28.537 回答