Will Strohl
Will Strohl

Reputation: 1680

Iterate through list in paged manner, within another loop

I have three collections. First, a collection of days. Next, a collection of time spans in each day. These time spans are the same for each day. Next, I have a collection of sessions.

There are 4 days. There are 6 time spans. There are 30 sessions.

I need to iterate through each day, assigning all of the time spans to each day the same way for each day. However, I need to assign the sessions to time blocks in sequence. For example, day 1 gets all 6 time spans, but only the first 6 sessions, 1-6. Day 2 gets the same time spans, but gets the next 6 sessions, 7-12.

How can I do this within the same method?

Here's what I have so far, but I'm having trouble wrapping my head around the paged iteration part.

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

Upvotes: 0

Views: 73

Answers (1)

Will Strohl
Will Strohl

Reputation: 1680

I ended up using LINQ in a new method based upon the GetItemsByTimeSlot() method. The new signature and example of getting a matching subset of that collection is below.

Here's how I'm calling it:

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

Here's what it looks like:

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

Upvotes: -1

Related Questions