private static Dictionary<int, NowAndNext> BuildNowNextFromDataSet(DataSet dataSet)
{
Dictionary<int, NowAndNext> progList = new Dictionary<int, NowAndNext>();
int programsCount = dataSet.Tables[0].Rows.Count;
List<int> lastChannelIDs = new List<int>();
// for-loops are faster than foreach-loops
for (int j = 0; j < programsCount; j++)
{
int idChannel = (int)dataSet.Tables[0].Rows[j]["idChannel"];
// Only get the Now-Next-Data _once_ per channel
if (!lastChannelIDs.Contains(idChannel))
{
lastChannelIDs.Add(idChannel);
int nowidProgram = (int)dataSet.Tables[0].Rows[j]["idProgram"];
DateTime nowStart = (DateTime)dataSet.Tables[0].Rows[j]["startTime"];
DateTime nowEnd = (DateTime)dataSet.Tables[0].Rows[j]["endTime"];
string nowTitle = (string)dataSet.Tables[0].Rows[j]["title"];
string episodeName = (string)dataSet.Tables[0].Rows[j]["episodeName"];
string seriesNum = (string)dataSet.Tables[0].Rows[j]["seriesNum"];
string episodeNum = (string)dataSet.Tables[0].Rows[j]["episodeNum"];
string episodePart = (string)dataSet.Tables[0].Rows[j]["episodePart"];
// if the first entry is not valid for the "Now" entry - use if for "Next" info
if (nowStart > DateTime.Now)
{
NowAndNext p = new NowAndNext(idChannel, SqlDateTime.MinValue.Value, SqlDateTime.MinValue.Value,
SqlDateTime.MinValue.Value, SqlDateTime.MinValue.Value,
string.Empty, nowTitle, -1, nowidProgram, episodeName, string.Empty,
seriesNum, string.Empty, episodeNum, string.Empty,
episodePart, string.Empty);
progList[idChannel] = p;
continue;
}
if (j < programsCount - 1)
{
// get the the "Next" info if it belongs to the same channel.
if (idChannel == (int)dataSet.Tables[0].Rows[j + 1]["idChannel"])
{
int nextidProgram = (int)dataSet.Tables[0].Rows[j + 1]["idProgram"];
DateTime nextStart = (DateTime)dataSet.Tables[0].Rows[j + 1]["startTime"];
DateTime nextEnd = (DateTime)dataSet.Tables[0].Rows[j + 1]["endTime"];
string nextTitle = (string)dataSet.Tables[0].Rows[j + 1]["title"];
string nextEpisodeName = (string)dataSet.Tables[0].Rows[j + 1]["episodeName"];
string nextSeriesNum = (string)dataSet.Tables[0].Rows[j + 1]["seriesNum"];
string nextEpisodeNum = (string)dataSet.Tables[0].Rows[j + 1]["episodeNum"];
string nextEpisodePart = (string)dataSet.Tables[0].Rows[j + 1]["episodePart"];
NowAndNext p = new NowAndNext(idChannel, nowStart, nowEnd, nextStart, nextEnd,
nowTitle, nextTitle, nowidProgram,
nextidProgram, episodeName, nextEpisodeName, seriesNum, nextSeriesNum,
episodeNum, nextEpisodeNum, episodePart, nextEpisodePart);
progList[idChannel] = p;
}
else
{
// no "next" info because of holes in EPG data - we want the "now" info nevertheless
NowAndNext p = new NowAndNext(idChannel, nowStart, nowEnd, SqlDateTime.MinValue.Value, SqlDateTime.MinValue.Value,
nowTitle, string.Empty, nowidProgram, -1,
string.Empty, string.Empty, string.Empty, string.Empty, string.Empty,
string.Empty, string.Empty, string.Empty);
progList[idChannel] = p;
}
}
}
}
return progList;
}