Forgive my disgusting method: I haven't been able to do this in SQL (as my use is dynamic and I'm not versed in the ways of SQL enough to have it create the dynamic output I'm wanting/needing) or EF as I haven't been able to get it to work for some unknown reason (if anyone has an example that may help a beginner with EF and DataTables please share)
I have a DataTable (dt) with values that I'm trying to pull, calculate and then input into another DataTable (fDt).
Example of dt:
+-------------------------------------------------------+
| ID | CustName | 201501 | 201502 | 201503 | 201504 | ..|
+-------------------------------------------------------+
| 32 | CustOne | 100.00 | 200.00 | 400.00 | 700.00 | ..|
| 56 | CustTwo | | | | 500.00 | ..|
| 89 | CustThree| 222.22 | 333.33 | 444.44 | 555.55 | ..|
| .. | ... | .. | .. | .. | .. | ..|
+-------------------------------------------------------+
I'm wanting to take the values after CustName to use in my calculation.
The calculation then gives the percent difference between two of the columns from the previous table:
+-------------------------------------------------------+
| ID | CustName | PerDiff02 | PerDiff03 | PerDiff04 | ..|
+-------------------------------------------------------+
| 32 | CustOne | 0 | 100 | 200 | ..|
| 56 | CustTwo | | | 85.00 | ..|
| 89 | CustThree| 66.66 | 75.00 | 80.00 | ..|
| .. | ... | .... | .... | .... | ..|
+-------------------------------------------------------+
(The percentages are spoofed, but they show what I'm trying to achieve.)
The PerDiffs should start from the second month shown (201502) and continue to current.
I believe I've done this, however with the code I have now:
for (i = 2; i <= (dt.Columns.Count - 2); i++)
{
for (int j = 0; j < (dt.Columns.Count); j++)
{
//decimal? month1 = dt.Rows[i].Field<decimal?>(j);
//decimal? month2 = dt.Rows[i].Field<decimal?>(j + 2);
decimal? month1 = (decimal?)dt.Rows[i][j];
decimal? month2 = (decimal?)dt.Rows[i][j + 2];
fDt.Rows[i][j - 1] = ((month1 - month2) / month1) * 100;
}
}
I have included two commented out lines as they produce the same error that I'm currently getting with this loop:
Specified cast is not valid.
For the lines declaring month1 and month2.
QUESTION:
How can I take the data from one of my Cells in one DataTable in an equation, and then input into another DataTable?
If anything is unclear, please let me know!
EDIT:
Here is my entire Method for getting my dataTable:
public DataTable GetPerDiff(DataTable dt)
{
var fDt = new DataTable();
int i;
int fieldCount = dt.Columns.Count;
string[] colHeaders = new string[fieldCount];
for (i = 0; i < fieldCount; i++)
{
colHeaders[i] = dt.Columns[i].ToString();
}
fDt.Columns.Add(colHeaders[0]);
fDt.Columns.Add(colHeaders[1]);
//Get's the data into the new table
for (i = 1; i < dt.Rows.Count-1; i++)
{
fDt.Rows.Add(dt.Rows[i][0], dt.Rows[i][1]);
}
// Gets the column headers for dataTable fDt
for (i = 2; i <= (dt.Columns.Count - 2); i++)
{
string colName = "PerDiff" + dt.Columns[i + 1];
fDt.Columns.Add(colName);
}
for (i = 2; i <= (dt.Columns.Count - 2); i++)
{
for (int j = 0; j < (dt.Columns.Count); j++)
{
//decimal? month1 = dt.Rows[i].Field<decimal?>(j);
//decimal? month2 = dt.Rows[i].Field<decimal?>(j + 2);
decimal? month1 = (decimal?)dt.Rows[i][j];
decimal? month2 = (decimal?)dt.Rows[i][j + 2];
fDt.Rows[i][j - 1] = ((month1 - month2) / month1) * 100;
}
}
return dt;
}