对于asp .net(C#)中的循环 [英] For loop in asp .net (C#)

查看:78
本文介绍了对于asp .net(C#)中的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好每一个



我有一个2100记录以上的DataTable。我想运行一个500个数据的循环。



因此,如果我有一个1000个数据,那么我的循环将是2个,或者我有一个1500个数据,而不是我的循环是b / 3


i已经完成了这么多代码但是不起作用



在下面的代码中dtRecepient是数据表

Hello Every one

I have a DataTable of 2100 records or more. I want to run a loop of 500 data.

So if i have a 1000 data than my loop will be of 2 or i have a 1500 data than my loop will be of 3

i have done this much of code but does't work

In below code dtRecepient is DataTable

for (int recepient = 0; recepient < dtRecepients.Rows.Count; recepient++)
{

}

推荐答案

如果你的意思是你必须做多少500个循环,比如说, n 然后在这里记录:

If you mean how many loops of 500 you have to do on, say, n records then here you are:
int loops = n / 500;
int spare = n % 500;





备用为您提供剩余记录的数量,如果 n 是不完全被500整除(例如 n = 2100 => spare = 100 )。



spare gives you the number of the remaining records if n is not exactly divisible by 500 (e.g. n=2100 => spare=100).


int total = dtRecepients.Rows.Count;
int iterations = total/500;

for (int recepient = 1; recepient <= iterations; recepient++)
{

}


使用这种方法获得所需的输出,



int startIndex,endIndex,totalCount;

totalCount = dtRecepients.Rows.Count;

startIndex = 0; endIndex = 500;

if(totalCount< endIndex)

{

for(startIndex; startIndex}

否则

{

DataTable dtDataTemp = dtRecepients.Clone();

while(endIndex< totalCount)

{

dtDataTemp.Rows.Clear();

if(endIndex> totalCount)

{endIndex = totalCount;}

for(int stIndex = startIndex; stIndex< endIndex; stIndex ++)

{

//做你的东西

}

startIndex = endIndex;

endIndex + = 500;

}

if(startIndex< totalCount && totalCount< endIndex)

{

dtDataTemp.Rows.Clear();

if(endIndex> totalCount)

{endIndex = totalCount; } $ / b
for(int stIndex = startIndex; stIndex< endIndex; stIndex ++)

{

//做你的东西

}

startIndex = endIndex;

endIndex + = 500;

}

}

}

其他

{}
use this approach to get the desired output,

int startIndex, endIndex, totalCount;
totalCount = dtRecepients.Rows.Count;
startIndex = 0; endIndex = 500;
if (totalCount < endIndex)
{
for(startIndex; startIndex }
else
{
DataTable dtDataTemp = dtRecepients.Clone();
while (endIndex < totalCount)
{
dtDataTemp.Rows.Clear();
if (endIndex > totalCount)
{ endIndex = totalCount; }
for (int stIndex = startIndex; stIndex < endIndex; stIndex++)
{
//do your stuff
}
startIndex = endIndex;
endIndex += 500;
}
if (startIndex < totalCount && totalCount < endIndex)
{
dtDataTemp.Rows.Clear();
if (endIndex > totalCount)
{ endIndex = totalCount; }
for (int stIndex = startIndex; stIndex < endIndex; stIndex++)
{
//do your stuff
}
startIndex = endIndex;
endIndex += 500;
}
}
}
else
{ }


这篇关于对于asp .net(C#)中的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆