在另一个循环中以分页方式迭代列表

问题描述 投票:0回答:1

我有三个收藏。 首先,收集几天。 接下来,每天的时间跨度的集合。 这些时间跨度每天都是相同的。 接下来,我有一个会话集合。

还有4天。 有6个时间跨度。 有30节课。

我需要迭代每一天,以相同的方式将所有时间跨度分配给每一天。 但是,我需要将会话按顺序分配给时间块。 例如,第 1 天获取所有 6 个时间跨度,但仅获取前 6 个会话(1-6)。 第 2 天的时间跨度相同,但有接下来的 6 个会话,即 7-12。

我怎样才能在同一个方法中做到这一点?

这是我到目前为止所拥有的,但我很难理解分页迭代部分。

var timeSlots = TimeSlotDataAccess.GetItems(codeCampId);
var assignableSlotCount = timeSlots.Where(t => !t.SpanAllTracks);

// determine how many days the event lasts for
agenda.NumberOfDays = (int)(agenda.CodeCamp.EndDate - agenda.CodeCamp.BeginDate).TotalDays;

// iterate through each day
agenda.EventDays = new List<EventDayInfo>(agenda.NumberOfDays);

var dayCount = 0;
while (dayCount <= agenda.NumberOfDays)
{
    var eventDate = agenda.CodeCamp.BeginDate.AddDays(dayCount);

    var eventDay = new EventDayInfo()
    {
        Index = dayCount,
        Day = eventDate.Day,
        Month = eventDate.Month,
        Year = eventDate.Year,
        TimeStamp = eventDate
    };

    // iterate through each timeslot
    foreach (var timeSlot in timeSlots)
    {
        var slot = new AgendaTimeSlotInfo(timeSlot);

        // iterate through each session
        // first day gets the first set of assignableTimeSlotCount, then the next iteration gets the next set of that count, etc.
        slot.Sessions = SessionDataAccess.GetItemsByTimeSlotId(slot.TimeSlotId, codeCampId).ToList();

        // iterate through each speaker
        foreach (var session in slot.Sessions)
        {
            session.Speakers=SpeakerDataAccess.GetSpeakersForCollection(session.SessionId, codeCampId);
        }
    }

    agenda.EventDays.Add(eventDay);

    dayCount++;
}
c# arrays pagination nested-lists
1个回答
0
投票

我最终在基于

GetItemsByTimeSlot()
方法的新方法中使用 LINQ。 下面是新签名和获取该集合的匹配子集的示例。

我是这样称呼它的:

slot.Sessions = SessionDataAccess.GetItemsByTimeSlotIdByPage(slot.TimeSlotId, 
codeCampId, dayCount + 1, timeSlotCount).ToList();

它看起来像这样:

public IEnumerable<SessionInfo> GetItemsByTimeSlotIdByPage(int timeSlotId, int codeCampId, int pageNumber, int pageSize)
{
    var items = repo.GetItems(codeCampId).Where(t => t.TimeSlotId == timeSlotId);

    items.Select(s => { s.RegistrantCount = GetRegistrantCount(s.SessionId); return s; });

    // this is the important part
    var resultSet = items.Skip(pageSize * (pageNumber - 1)).Take(pageSize);

    foreach (var item in resultSet)
    {
        item.Speakers = speakerRepo.GetSpeakersForCollection(item.SessionId, item.CodeCampId);
    }

    return resultSet;
}
© www.soinside.com 2019 - 2024. All rights reserved.