繁体   English   中英

使用 linq 按日期分组

[英]Grouping by date using linq

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

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

想要的结果是 3 个组(参见 ManualGrouping)

有什么建议么?

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

}

你可以这样做:

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

您可以通过AppointmentDatetempAppointmentList进行分组后获取组键和约会列表,然后使用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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM