如何循环50行50行 [英] How to loop 50 rows by 50 rows
问题描述
我有一个代码,我将从数据库中获取一堆数据(例如102行记录)。然后我将循环每个数据行的数据表,并使用foreach循环将它们连接成50行50行的长字符串值。
我的问题是在获得第50行记录之后,我该如何继续其余的记录(第51次以上)?
我的代码如下:
Hi, I have a codes in which I will get a bunch of data from database(e.g. 102 rows of records). Then I will loop the datatable for each data row and concatenate them into a long string value 50 rows by 50 rows by using foreach loop.
My problem is after getting 50th row of records, how can I continue for the remaining records(51st onwards)?
My codes are as below:
dt_top = da.getTopLevelAssm(site_id, mpf);//102 rows of records
string value = string.Empty;
int count = 1;
foreach (DataRow dr_top in dt_top.Rows)//looping for each records
{
if (dt_top.Rows.IndexOf(dr_top) == 0)//first record
{
value = dr_top["ixkit"].ToString();
count++;
}
else//subsequent record with a comma added behind each record
{
value = value + "," + dr_top["ixkit"].ToString();
count++;
}
if (count == 50)//break at 50th record to prevent from further looping
{
break;
}
}
我的尝试:
1.试图回调一个循环但找不到方法。
What I have tried:
1. Tried to call back a loop but cannot find a way.
推荐答案
使用StringBuilder,并且为 - loop:
Use a StringBuilder, and a for-loop:
StringBuilder sb = new StringBuilder();
// better to declare the actual Type here
var dt_top = da.getTopLevelAssm(site_id, mpf);//102 rows of records
DataRow dr;
for (int i = 0; i < dt_top.Rows.Count; i++) // for each record
{
dr = dt_top.Rows[i];
if (i < 50) //first 50 records
{
sb.AppendFormat("{0},", dr["ixkit"]);
}
else
{
// handle the rest of the rows
}
}
// trim off the final comma
sb.Length -= 1;
// get the string out of the StringBuilder
string result = sb.ToString();
使用StringBuilder减少了在这样的代码中不必要地创建新字符串。在这里使用'for-loop也会改善执行时间,例如,取消对'IndexOf的调用。
Using a StringBuilder reduces the unnecessary creation of new strings in code like this. Using a 'for-loop here will also improve execution time by, for example, eliminating the call to 'IndexOf.
你可以使用嵌套的来
循环处理行50乘50:
You can use a nestedfor
loop to process the rows 50 by 50:
for (int i = 0; i < (int)Math.Ceiling(dt_top.Rows.Count / 50F); i++)
{
for (int j = i * 50; j < (i + 1) * 50 && j < dt_top.Rows.Count; j++)
{
DataRow dr_top = dt_top.Rows[j];
// do what you need to do with the row
}
// do what you need to do after processing a batch of 50 rows
}
第一件事是在适应第二批50之前让这段代码正常工作。
您应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。
调试器允许你跟踪执行逐行检查变量,你会看到有一点它会停止你所期望的。
在Visual Studio 2010中掌握调试 - 初学者指南 [ ^ ]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]
您的代码没有显示您使用前50行的值
执行的操作。
此代码完全错误。
The first thing is to make this code work correctly before adapt for the second batch of 50.
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.
The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
Your code don't show what you do withvalue
of first 50 rows.
This code is plain wrong.
value = ","+ value + dr_top["ixkit"].ToString();
你运行代码吗?
[更新到SownVoter]
不要晕,解释你不喜欢的地方。
Have you run the code ?
[Update to SownVoter]
Don't be faint, explain what you don't like here.
这篇关于如何循环50行50行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!