I need to group some data so that it can display nicely in my page.
Given a list of appointments I need to group them by day.
the wanted result is 3 groups (See ManualGrouping)
Any suggestions?
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; }
}
}
You can do it like this:
var appointmentGroups = tempAppointmentList
.GroupBy(a => a.AppointmentDate)
.Select(g => new AppointmentGroup(g.Key, g.ToList()));
You can get a group key and list of appointments after grouping tempAppointmentList
by AppointmentDate
, then create a new AppointmentGroup
instance using Select
method
var tempAppointmentList = GetAppointmentList();
Appointments = tempAppointmentList
.GroupBy(a => a.AppointmentDate)
.Select(g => new AppointmentGroup(g.Key, g.ToList()))
.ToList();
It'll work since every group has IGrouping<DateTime, AppointmentViewModel>
type and inherits IEnumerable<AppointmentViewModel>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.