我试图“收集”GetString(2),直到GetString(0)发生变化,所以我试图找出如何将“集合”与创建和添加“lookaheadRunInfo”的新实例分开?我试过了如下所示抛出异常
if (dtext[i] in CODE.keys()):
enc += CODE[dtext[i]]
在线
System.NullReferenceException was unhandled by user code
,是否有人可以就如何解决此问题提供指导?
lookaheadRunInfo.gerrits.Add(rdr.GetString(1));
答案 0 :(得分:1)
如果我正确理解了您的要求,您希望为结果集的多行保留一个lookaheadRunInfo
,直到GetString(0)发生更改。是吗?
在这种情况下,您有一些重大的逻辑问题。它的编写方式,即使我们修复了null引用,每行都会得到一个新的lookaheadRunInfo
。
试试这个:
string ECJoblink_previous = null;
LookAheadRunInfo lookaheadRunInfo = null;
while (rdr.Read())
{
if (ECJoblink_previous != rdr.GetString(0)) //A new set of rows is starting
{
if (lookaheadRunInfo != null)
{
lookaheadRunsInfo.Add(lookaheadRunInfo); //Save the old group, if it exists
}
lookaheadRunInfo = new LookAheadRunInfo //Start a new group and initialize it
{
ECJobLink = rdr.GetString(0),
gerrits = new List<string>(),
UserSubmitted = rdr.GetString(2),
SubmittedTime = rdr.GetString(3).ToString(),
RunStatus = "null",
ElapsedTime = (DateTime.UtcNow-rdr.GetDateTime(3)).ToString()
}
}
lookahead.gerrits.Add(rdr.GetString(1)); //Add current row
ECJoblink_previous = rdr.GetString(0); //Keep track of column 0 for next iteration
}
if (lookaheadRunInfo != null)
{
lookaheadRunsInfo.Add(lookaheadRunInfo); //Save the last group, if there is one
}
这里的想法是: