编写 for 循环,具有与 foreach 相同的功能
本文关键字:foreach 功能 for 循环 编写 | 更新日期: 2023-09-27 17:55:46
我有我的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 循环匹配?
您需要获取每次迭代的数据行。
在 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);
}
首先,
你用错了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
}
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();