按日期分组列表,每个项目有多个日期

时间:2016-08-16 04:13:34

标签: c# android linq group-by xamarin.android

我有一个如下列表: -

列表

编辑(生日和纪念日的年份可能不同)

- EmpGuid -265d317b-b819-4171-ba12-e64388746d81
Name - abc
Birthday - 15 Aug 2000
Anniversary- 12 july 1989

EmpGuid - 265d317b-b819-4171-ba12-e64388746d82
Name - xyz
Birthday - 24 Jan 2000
Anniversary- 15 Aug 1988

EmpGuid - 265d317b-b819-4171-ba12-e64388746d83
Name - mno
Birthday - 15 aug 2000
Anniversary- 24 Jan 1987

我想根据日期对列表进行分组: -

12 July - abc anniversary

15 aug - abc Birthday
         mno Birthday
         xyz Anniversary

24 jan - xyz birthday
         mno anniversary

我试过这样做: -

 var groupedEmpList = FinalList.GroupBy(u => u.Birthdate)
                                      .Select(grp =>new { GroupID =grp.Key, FinalList = grp.ToList()})
                                      .ToList()

以上并没有给我想要的输出。任何有关这方面的帮助将不胜感激

2 个答案:

答案 0 :(得分:1)

此解决方案使用SelectMany获取您可以按项目分组的所有日期的展开列表:

var result = FinalList.Select(item => new 
                      { 
                          Date = new [] { item.Birthday.ToString("ddMM"), item.Anniversary.ToString("ddMM") }, 
                          Item = item 
                      })
                      .SelectMany(item => item.Date.Select(date => new { Date = date, Item = item.Item }))
                      .GroupBy(item => item.Date)
                      .Select(grouping => new { Date = grouping.Key, Events = grouping.ToList() }).ToList();

还可以在select内执行第一个SelectMany - 为了答案,我将其单独保存

用于添加事件的类型(以及删除第一个select的方式):

var result = FinalList.SelectMany(item => new List<dynamic>
                        {
                            new { Date = item.Birthday.ToString("ddMM"), Event = "Birthday", Item = item },
                            new { Date = item.Anniversary.ToString("ddMM"), Event = "Anniversary", Item = item }
                        })
                      .GroupBy(item => item.Date)
                      .Select(grouping => new { Date = grouping.Key, Events = grouping.ToList() }).ToList();

要输出这些结果,您可以:

public enum EventTypes
{
    Birthday,
    Anniversary
}

public class Event
{
    public string Date { get; set; }
    public EventTypes Type { get; set; }
    public IEnumerable<dynamic> Items { get; set; }
}

var result = FinalList.SelectMany(item => new List<dynamic>
                        {
                            new { Date = item.Birthday.ToString("ddMM"), Type = EventTypes.Birthday, Item = item },
                            new { Date = item.Anniversary.ToString("ddMM"), Type = EventTypes.Anniversary, Item = item }
                        })
                      .GroupBy(item => new { item.Date, item.Type })
                      .Select(grouping => new Event { Date = grouping.Key.Date, Type = grouping.Key.Type, Items = grouping.ToList() }).ToList();

现在result列表为List<Event>,您在Items内也有您的oridinal对象(将该列表的dynamic替换为原始类类型 - 我只是不知道它)

答案 1 :(得分:0)

假设你有一个班级:

class Info
{
    public Guid EmpGuid { get; set; }
    public string Name { get; set; }
    public DateTime Birthday { get; set; }
    public DateTime Anniversary { get; set; }
}

您需要将您的课程拆分为以下课程:

class UnpivotInfo
{
    public Guid EmpGuid { get; set; }
    public string Name { get; set; }
    public DateTime Date { get; set; }
    public string Type { get; set; }
}

你的类型是“生日”或“周年纪念日”,通过这样做:

var unpivoted = list.SelectMany(i => new[]
{
    new UnpivotInfo {EmpGuid = i.EmpGuid, Date = i.Birthday, Type = "Birthday", Name = i.Name}
    , new UnpivotInfo {EmpGuid = i.EmpGuid, Date = i.Anniversary, Type = "Anniversary", Name = i.Name}
});

然后,您可以按日期对数据进行分组:

var groups = unpivoted.GroupBy(p => p.Date);