C# 如何在一个foreach中跳过结果的迭代
我有两个C# 如何在一个foreach中跳过结果的迭代,c#,asp.net,foreach,C#,Asp.net,Foreach,我有两个foreach循环,如代码所示: DataTable dtx = GetAmens(Id); if (dtx.Rows.Count > 0) { strAmen += @"<ul class='col-md-6'>"; int c = 0; foreach (DataRow dr in dtx.Rows) { if (c >= ((dtx.Rows.Count / 2) + (dtx.Rows.Count % 2))
foreach
循环,如代码所示:
DataTable dtx = GetAmens(Id);
if (dtx.Rows.Count > 0)
{
strAmen += @"<ul class='col-md-6'>";
int c = 0;
foreach (DataRow dr in dtx.Rows)
{
if (c >= ((dtx.Rows.Count / 2) + (dtx.Rows.Count % 2))) break;
strAmen += "<li class='enabled'>" + dr["amen"].ToString() + "</li>";
c++;
}
strAmen += @"</ul><ul class='col-md-6'>";
c = 0;
foreach (DataRow dr in dtx.Rows)
{
strAmen += "<li class='enabled'>" + dr["amen"].ToString() + "</li>";
c++;
}
strAmen += @"</ul>";
}
DataTable dtx=GetAmens(Id);
如果(dtx.Rows.Count>0)
{
strAmen+=@“”;
int c=0;
foreach(dtx.Rows中的数据行dr)
{
如果(c>=((dtx.Rows.Count/2)+(dtx.Rows.Count%2))中断;
strAmen++“- ”+dr[“amen”]。ToString()+“
”;
C++;
}
斯特拉曼+=@“
”;
c=0;
foreach(dtx.Rows中的数据行dr)
{
strAmen++“- ”+dr[“amen”]。ToString()+“
”;
C++;
}
斯特拉曼+=@“
”;
}
数据表中有7个值
第一个foreach
迭代4个元素
现在我希望第二个foreach
语句不能遍历这4个元素(结果出现在第一个foreach
语句中)
而是遍历其余的3个元素
有人能建议进行任何必要的更改以获得这样的结果吗???
提前谢谢 您可以使用LINQ将组一分为二。然后检查组并将其逐个添加到结果中:
var columns = dtx.Rows.OfType<DataRow>().Select((r, i) => new {Row = r, Group = i >= ((dtx.Rows.Count / 2) + (dtx.Rows.Count % 2))}).GroupBy(r => r.Group);
foreach (var group in columns)
{
strAmen += @"<ul class='col-md-6'>";
foreach (DataRow dr in group.Select(g => g.Row))
{
strAmen += "<li class='enabled'>" + dr["amen"].ToString() + "</li>";
}
strAmen += @"</ul>";
}
var columns=dtx.Rows.OfType().Select((r,i)=>new{Row=r,Group=i>=((dtx.Rows.Count/2)+(dtx.Rows.Count%2)).GroupBy(r=>r.Group);
foreach(列中的var组)
{
strAmen+=@“”;
foreach(组中的数据行dr.Select(g=>g.Row))
{
strAmen++“- ”+dr[“amen”]。ToString()+“
”;
}
斯特拉曼+=@“
”;
}
一个简单的解决方案是使用一个新变量,如果可以的话,使用for循环。
这将确保只需循环“n”次(n是记录数)
如果您对变量“c”有一些逻辑,那么在内部for循环中使用一个新变量,而不是重用“c”
DataTable dtx=GetAmens(Id);
字符串strimen=“”;
如果(dtx.Rows.Count>0)
{
strAmen+=@“”;
int c=0;
foreach(dtx.Rows中的数据行dr)
{
如果(c>=((dtx.Rows.Count/2)+(dtx.Rows.Count%2)))
{
斯特拉曼+=@“
”;
对于(c=c;c”+dtx.Rows[c][“amen”].ToString()+“”;
}
斯特拉曼+=@“
”;
打破
}
strAmen++“”+dr[“amen”]。ToString()+“ ”;
C++;
}
}
为什么不使用常规for循环呢?将foreach
替换为for
。如果您确实想保留foreach
,并且不再需要使用这些元素,在第一个foreach中迭代后,您可能会从数据表中删除这些第一个元素?@Isuka我需要所有这些元素。在第一个foreach中迭代后,是否可能从数据表中删除这些第一个元素&在第二个foreach中迭代剩余的元素?如果是,那怎么做?我对你的代码做了正确的编辑?第一行对结果进行分组。然后它基本上和你的代码一样。你错过了一个角括号,我加了它。但现在它显示了以下错误:“datarow不包含select的定义”。您可以重新检查您的编辑吗?您仍然无法关闭此处的角括号:var columns=dtx.Rows.OfType()。选择((r,i)=>new{Row=r,Group=i>=((dtx.Rows.Count/2)+(dtx.Rows.Count%2))。GroupBy(r=>Group);当我进行更正时,它说:“foreach语句无法对类型的变量进行操作?”谢谢,但是它不能像visual studio那样运行,抛出“Group是一种类型,在这种上下文中无效”以及“foreach语句不能对类型的变量进行操作?”Argh,如果没有VS,它就不能正确键入。现在它应该可以了。
DataTable dtx = GetAmens(Id);
string strAmen = "";
if (dtx.Rows.Count > 0)
{
strAmen += @"<ul class='col-md-6'>";
int c = 0;
foreach (DataRow dr in dtx.Rows)
{
if (c >= ((dtx.Rows.Count / 2) + (dtx.Rows.Count % 2)))
{
strAmen += @"</ul><ul class='col-md-6'>";
for (c=c; c < dtx.Rows.Count; c++)
{
strAmen += "<li class='enabled'>" + dtx.Rows[c]["amen"].ToString() + "</li>";
}
strAmen += @"</ul>";
break;
}
strAmen += "<li class='enabled'>" + dr["amen"].ToString() + "</li>";
c++;
}
}