使用linq按日期分组

时间:2020-05-22 10:46:36

标签: c# linq

我需要对一些数据进行分组,以便它们可以在我的页面中很好地显示。

给出一个约会列表,我需要按天对它们进行分组。

所需结果为3组(请参见手册分组)

有什么建议吗?

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleGrouping
{      
 class Program
 {
    static void Main()
    {
        var groupedAppointments = new GroupedAppointments();
        var myAppointments=groupedAppointments.Appointments;

    }
}

public class GroupedAppointments
{
    public List<AppointmentGroup> Appointments { get; private set; } = new List<AppointmentGroup>();

    public GroupedAppointments()
    {
        //ManualGrouping(); this is the wanted result

        var tempAppointmentList = GetAppointmentList();

        //now I have tried few linq query to get the same result as ManualGrouping method. but I am getting the syntax wrong.
        //failed attempt
        //var Appointments = (from apt in tempAppointmentList
        //    group apt by apt.AppointmentDate into aptGroup
        //    select new AppointmentGroup(aptGroup.Key,???)
        //    {
        //        AppointmentDate = aptGroup.Key,
        //        Title = (from aa in aptGroup
        //            select aa.Title).ToList()
        //    }).ToList();
    }
    private List<AppointmentViewModel> GetAppointmentList()
    {
        var tempList = new List<AppointmentViewModel>
        {
            new AppointmentViewModel {AppointmentDate = DateTime.Today, Title = "See doctor"},
            new AppointmentViewModel {AppointmentDate = DateTime.Today, Title = "Call John"},

            new AppointmentViewModel {AppointmentDate = DateTime.Today.AddDays(-1), Title = "Go to Supermarket"},
            new AppointmentViewModel {AppointmentDate = DateTime.Today.AddDays(-1), Title = "Go to work"},

            new AppointmentViewModel {AppointmentDate = DateTime.Today.AddDays(-2), Title = "Go Shopping"},
            new AppointmentViewModel {AppointmentDate = DateTime.Today.AddDays(-2), Title = "Go to Dentist"}
        };
        return tempList;
    }
    private void ManualGrouping()
    {
        Appointments.Add(new AppointmentGroup(DateTime.Today, new List<AppointmentViewModel>
        {
            new AppointmentViewModel {AppointmentDate = DateTime.Today, Title = "See doctor"},
            new AppointmentViewModel {AppointmentDate = DateTime.Today, Title = "Call John"},
            new AppointmentViewModel {AppointmentDate = DateTime.Today, Title = "Call Mary"},
        }));
        Appointments.Add(new AppointmentGroup(DateTime.Today.AddDays(-1), new List<AppointmentViewModel>
        {
            new AppointmentViewModel {AppointmentDate = DateTime.Today.AddDays(-1), Title = "Go to Supermarket"},
            new AppointmentViewModel {AppointmentDate = DateTime.Today.AddDays(-1), Title = "Go to work"},
        }));
        Appointments.Add(new AppointmentGroup(DateTime.Today.AddDays(-1), new List<AppointmentViewModel>
        {
            new AppointmentViewModel {AppointmentDate = DateTime.Today.AddDays(-2), Title = "Go Shopping"},
            new AppointmentViewModel {AppointmentDate = DateTime.Today.AddDays(-2), Title = "Go to Dentist"}
        }));
    }
}
public class AppointmentGroup : List<AppointmentViewModel>
{
    public DateTime AppointmentDate { get; private set; }

    public AppointmentGroup(DateTime appointmentDate, List<AppointmentViewModel> appointments)
        : base(appointments)
    {
        AppointmentDate = appointmentDate;
    }

    public override string ToString()
    {
        return AppointmentDate.ToString();
    }
}

public class AppointmentViewModel
{
    public string Title { get; set; }
    public DateTime AppointmentDate { get; set; }
}

}

2 个答案:

答案 0 :(得分:1)

您可以这样做:

var appointmentGroups = tempAppointmentList
    .GroupBy(a => a.AppointmentDate)
    .Select(g => new AppointmentGroup(g.Key, g.ToList()));

答案 1 :(得分:0)

通过tempAppointmentListAppointmentDate进行分组之后,您可以获取组密钥和约会列表,然后使用Select方法创建新的AppointmentGroup实例

var tempAppointmentList = GetAppointmentList();
Appointments = tempAppointmentList
    .GroupBy(a => a.AppointmentDate)
    .Select(g => new AppointmentGroup(g.Key, g.ToList()))
    .ToList();

它将起作用,因为每个组都具有IGrouping<DateTime, AppointmentViewModel>类型并继承了IEnumerable<AppointmentViewModel>