使用与foreach相同的函数编写for循环

时间:2014-03-31 07:43:46

标签: c# loops

我有我的foreach循环:

var i = 0;
foreach (DataRow data in result.Tables[0].Rows)
{
     var periodStartDate = Convert.ToDateTime(data.Table.Rows[i]["Date"].ToString().Remove(10));
     var periodEndDate = Convert.ToDateTime(data.Table.Rows[i]["Date"].ToString().Remove(0, 12));
     calc.PeriodStartDate = periodStartDate;
     calc.PeriodEndDate = periodEndDate;
     calc.InvoiceAmount = Convert.ToDecimal(data.Table.Rows[i]["Invoice amount"].ToString());
     calc.InterestRate = Convert.ToDecimal(data.Table.Rows[i]["Interest rate"].ToString());
     calc.InterestAmount = Convert.ToDecimal(data.Table.Rows[i]["Interest amount"].ToString());
     calc.Amortization = Convert.ToDecimal(data.Table.Rows[i]["Amortization"].ToString());
     calc.PresentValue = Convert.ToDecimal(data.Table.Rows[i]["Capital balance"].ToString());
     calc.StartValue = Convert.ToDecimal(data.Table.Rows[0]["Capital balance"].ToString());
     cList.Add(calc);
     i++;
}

我想做同样的事情但是在for循环中。试过这个(不要工作,因为它无法找到数据(显然):

for (int i = 0; i < result.Tables[0].Rows.Count; i++)
{
     var periodStartDate = Convert.ToDateTime(data.Table.Rows[i]["Date"].ToString().Remove(10));
     var periodEndDate = Convert.ToDateTime(data.Table.Rows[i]["Date"].ToString().Remove(0, 12));
     calc.PeriodStartDate = periodStartDate;
     calc.PeriodEndDate = periodEndDate;
     calc.InvoiceAmount = Convert.ToDecimal(data.Table.Rows[i]["Invoice amount"].ToString());
     calc.InterestRate = Convert.ToDecimal(data.Table.Rows[i]["Interest rate"].ToString());
     calc.InterestAmount = Convert.ToDecimal(data.Table.Rows[i]["Interest amount"].ToString());
     calc.Amortization = Convert.ToDecimal(data.Table.Rows[i]["Amortization"].ToString());
     calc.PresentValue = Convert.ToDecimal(data.Table.Rows[i]["Capital balance"].ToString());
     calc.StartValue = Convert.ToDecimal(data.Table.Rows[0]["Capital balance"].ToString());
     cList.Add(calc);
}

如何重新编写for循环以使其与我的foreach循环匹配?

3 个答案:

答案 0 :(得分:1)

您需要为每次迭代获取DataRow。

在forloop中添加以下语句作为第一个语句:

DataRow data = result.Tables[0].Rows[i];

完整代码:

for (int i = 0; i < result.Tables[0].Rows.Count; i++)
{
     DataRow data = result.Tables[0].Rows[i];
     var periodStartDate = Convert.ToDateTime(data.Table.Rows[i]["Date"].ToString().Remove(10));
     var periodEndDate = Convert.ToDateTime(data.Table.Rows[i]["Date"].ToString().Remove(0, 12));
     calc.PeriodStartDate = periodStartDate;
     calc.PeriodEndDate = periodEndDate;
     calc.InvoiceAmount = Convert.ToDecimal(data.Table.Rows[i]["Invoice amount"].ToString());
     calc.InterestRate = Convert.ToDecimal(data.Table.Rows[i]["Interest rate"].ToString());
     calc.InterestAmount = Convert.ToDecimal(data.Table.Rows[i]["Interest amount"].ToString());
     calc.Amortization = Convert.ToDecimal(data.Table.Rows[i]["Amortization"].ToString());
     calc.PresentValue = Convert.ToDecimal(data.Table.Rows[i]["Capital balance"].ToString());
     calc.StartValue = Convert.ToDecimal(data.Table.Rows[0]["Capital balance"].ToString());
     cList.Add(calc);
}

答案 1 :(得分:1)

首先,你使用foreach本身就错了。你已经有了DataRow但仍然有一个柜台等等,他们不是必需的。

foreach (DataRow data in result.Tables[0].Rows)
{
     var periodStartDate = Convert.ToDateTime(data["Date"].ToString().Remove(10));
     //You can simplify rest like this with `data`
}

for (int i = 0; i < result.Tables[0].Rows.Count; i++)
{    
    DataRow data = result.Tables[0].Rows[i];
    var periodStartDate = Convert.ToDateTime(data["Date"].ToString().Remove(10)); 
    //And so on   
}

答案 2 :(得分:0)

var table = result.Tables[0];

for (int i = 0; i < table.Rows.Count; i++)
{
     var periodStartDate = Convert.ToDateTime(table.Rows[i]["Date"].ToString()
                                  .Remove(10));
     var periodEndDate   = Convert.ToDateTime(table.Rows[i]["Date"].ToString()
                                  .Remove(0, 12));

     // And the rest of the fields...

     cList.Add(calc);
}

或者,当然,你可以简单地使用LINQ:

var cList = 
  result.Tables[0].Rows.Select
  (
    (row, index) =>
    {
      var calc = new YourCalc();

      calc.Something = row["SomeField"] as SomeType;

      return calc;
    }
  ).ToList();