簡體   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