在 C# 中迭代​​数据行

Iterate datarow in c#

在Selected dataRow中我总共有200条数据记录行。现在我需要将200条记录分成20 [10次(for循环)]。所以我使用 take(20)。它需要前 20 条记录。

我需要删除最后的前20条记录,需要再选择20条records.I需要执行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. 
    }
}

您可以使用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