0

I have my foreach loop:

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++;
}

I wanna do the same but in a for loop instead. Tried this (don't work because it can't find data (obviously):

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);
}

How can I re-write my for loop so it matches my foreach loop?

5
  • 3
    Wait, what? You're using a foreach over the Rows so that you could take the table and then iterate over its rows using an indexer? Why aren't you simply doing data["Date"] etc. in your foreach code? Commented Mar 31, 2014 at 7:45
  • I didn't now that worked.. Commented Mar 31, 2014 at 7:46
  • Also, note that you have to create a new instance of calc in each iteration of the loop (unless it's a struct), otherwise you'll have a nice list of completely identical values in cList. And of course, there's no reason to convert the value to string and then to a given data type - you can cast it right away, eg. (decimal)row["Capital balance"]. Commented Mar 31, 2014 at 7:50
  • @Luaan When I use data["Date"] I get "Input string was not a correct format". So that doesn't work for me Commented Mar 31, 2014 at 8:14
  • Well, for a date, you'd obviously have to use (DateTime)data["Date"]. Assuming you do actually have a structure in the data table. If it's all made of strings, you're going to have to do the parsing (but you still should do a cast, rather than ToString). Commented Mar 31, 2014 at 8:17

3 Answers 3

1

You need to get the DataRow for each iteration .

Add the following statement in forloop as first statement:

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

Complete Code:

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);
}
Sign up to request clarification or add additional context in comments.

Comments

1

In first place you're using foreach itself wrong. You already have DataRow and still you're having a counter, etc etc they are not required.

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   
}

Comments

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);
}

Or, of course, you can simply use LINQ:

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

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

      return calc;
    }
  ).ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.