简体   繁体   English

在C#中迭代数据行

[英]Iterate datarow in c#

In Selected dataRow I have totally 200 data record rows. 在Selected dataRow中,我总共有200条数据记录行。 Now I need to divide the 200 records into 20 [10 times(for loop)]. 现在,我需要将200条记录分为20 [10次(用于循环)]。 so I use the take(20).It will take first 20 records. 所以我使用take(20),它将获取前20条记录。

I need to remove the first 20 records in last and need to choose another 20 records.I need to execute the 10 times for loop and get all the 200 records. 我需要删除最后的前20条记录,并选择另外20条记录。我需要执行10次for循环并获取所有200条记录。

DataRow[] selectedDataRow = dtSMSDetails.Select("description = '" + smsDescription + "'");

if (selectedDataRow.Length > 0)
{
    string smsRecordId = "";
    string mobileNum = "";
    string smsSubject = "";

    foreach (DataRow rows in selectedDataRow.Take(20)) 
    {
        smsRecordId += rows["activityid"].ToString() + ",";
        smsSubject = rows["subject"].ToString();
        mobileNum += rows["telephone1"].ToString() + ",";
        // Here I need to remove the first 20 (take 20) records from the selected data row and need to loop next 20 records. 
    }
}

You can use Skip() 您可以使用Skip()

int startIndex = 0;

for (int i = 0; i < 10; i++)
{
    foreach (DataRow rows in selectedDataRow.Skip(startIndex).Take(20))
    {
        smsRecordId += rows["activityid"].ToString() + ",";
        smsSubject = rows["subject"].ToString();
        mobileNum += rows["telephone1"].ToString() + ",";
    }

    startIndex += 20;
}

Read more 阅读更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM