Ale*_*ec. 0 c# foreach continue
我有以下代码
private void bgwSendMail_DoWork(object sender, DoWorkEventArgs e)
{
DataSet ds = getMailToSend();
DataTable table = ds.Tables[0];
{
foreach (DataRow row in table.Rows)
{
{
string attachment1 = ds.Tables[0].Rows[0]["Attachment1"].ToString();
string attachment2 = ds.Tables[0].Rows[0]["Attachment2"].ToString();
string attachment3 = ds.Tables[0].Rows[0]["Attachment3"].ToString();
string attachment4 = ds.Tables[0].Rows[0]["Attachment4"].ToString();
string mailTo = ds.Tables[0].Rows[0]["EmailTo"].ToString();
string mailSubject = ds.Tables[0].Rows[0]["EmailSubject"].ToString();
string mailBody= ds.Tables[0].Rows[0]["EmailBody"].ToString();
string uid = ds.Tables[0].Rows[0]["uid"].ToString();
if (String.IsNullOrEmpty(attachment1))
{
//TODO Send Email Straight away ignore rest
}
else
{
if (!String.IsNullOrEmpty(attachment1))
{
bool attachment1Exists = checkFileExists(attachment1);
if (attachment1Exists == false)
{
continue;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我希望,当我们在底部点击继续(确实被击中)时,我们应该退回到foreach,如下所示,然后转到数据集中的下一条记录
这种情况不会发生,它会反复遍历连续传来的相同记录,这是正常的吗?
如果这是正常的,foreach一旦退出一次,最好的方法是忽略数据表中的那一行?
这continue是按预期工作的.
您正在枚举表中的所有行,但您没有使用它.相反,您总是访问表中的第一行:
DataTable table = ds.Tables[0];
foreach(DataRow row in table.Rows)
{
string attachment1 = ds.Tables[0].Rows[0]["Attachment1"].ToString();
// ...
}
Run Code Online (Sandbox Code Playgroud)
你总是访问ds.Tables[0].Rows[0].
相反,你应该使用这个代码:
foreach(DataRow row in table.Rows)
{
string attachment1 = row["Attachment1"].ToString();
// ...
}
Run Code Online (Sandbox Code Playgroud)
所以你实际上是按照预期枚举表中的所有行,它不是一个无限循环,但你没有使用表中的每一行而只是第一行.