我使用此代码按人员名称对多个时隙对象进行分组。所以每个工作人员都有许多插槽。这些插槽目前间隔为15分钟。
IEnumerable<AvailabilityByStaffDto> groupedSlots =
from slots in slotsAvailability
group slots by slots.Staff.Name into g
select new AvailabilityByStaffDto()
{
Name = g.Key,
Slots = g.ToList()
};
是否可以按小时对这些插槽进行分组? 那么老虎机属性可能实际上是 SlotHour ,并且我们将拥有老虎机?
编辑:
好的,这就是我现在拥有的。
IEnumerable<AvailabilityByStaffDto> groupedSlots =
from slots in slotsAvailability
group slots by slots.Staff.Name into g
select new AvailabilityByStaffDto()
{
Name = g.Key,
Slots = (from b in g
group b by b.SlotStart.Hour into c
select new GroupedTimeslotDto()
{
Hour = c.Key,
QuarterSlots = c.ToList()
});
};
public class AvailabilityByStaffDto
{
public AvailabilityByStaffDto()
{
this.Slots = new List<DTO.GroupedTimeslotDto>();
}
public int StaffId { get; set; }
public string Name { get; set; }
public List<DTO.GroupedTimeslotDto> Slots { get; set; }
}
public class GroupedTimeslotDto
{
public GroupedTimeslotDto()
{
this.QuarterSlots = new List<DTO.TimeslotDto>();
}
public int Hour { get; set; }
public List<DTO.TimeslotDto> QuarterSlots { get; set; }
}
某处源类型IEnumerable无法转换为目标类型Generic.List。但是我不明白为什么会发生这种情况,我只是把我的第一个分组复制到一个新的水平......
编辑2:哦,好的。我想我忘了最后一个.ToList() IEnumerable<AvailabilityByStaffDto> groupedSlots =
from slots in slotsAvailability
group slots by slots.Staff.Name into g
select new AvailabilityByStaffDto()
{
Name = g.Key,
Slots = (from b in g
group b by b.SlotStart.Hour into c
select new GroupedTimeslotDto()
{
Hour = c.Key,
QuarterSlots = c.ToList()
}).ToList()
};
这可能有用。