c# - Iterate through list in paged manner, within another loop -
i have 3 collections. first, collection of days. next, collection of time spans in each day. these time spans same each day. next, have collection of sessions.
there 4 days. there 6 time spans. there 30 sessions.
i need iterate through each day, assigning of time spans each day same way each day. however, need assign sessions time blocks in sequence. example, day 1 gets 6 time spans, first 6 sessions, 1-6. day 2 gets same time spans, gets next 6 sessions, 7-12.
how can within same method?
here's have far, i'm having trouble wrapping head around paged iteration part.
var timeslots = timeslotdataaccess.getitems(codecampid); var assignableslotcount = timeslots.where(t => !t.spanalltracks); // determine how many days event lasts 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 first set of assignabletimeslotcount, next iteration gets next set of 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++; }
i ended using linq in new method based upon getitemsbytimeslot()
method. new signature , example of getting matching subset of collection below.
here's how i'm calling it:
slot.sessions = sessiondataaccess.getitemsbytimeslotidbypage(slot.timeslotid, codecampid, daycount + 1, timeslotcount).tolist();
here's 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; }); // 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; }
Comments
Post a Comment