I want to loop through table1 D_ID and find the same in table2 and then loop through column (match)and find the difference
Table 1
| D_ID | Split | Value_1 | Value_2 | Value_3 |
| N6K_12 | Str | 1.503 | 9.5E-03 | -0.48 |
| N6K_13 | Ref | 1.680 | 6.5E-03 | -0.63 |
| N6K_14 | St | 1.690 | 3.3E-03 | -0.43 |
| N6K_15 | Ref | 1.903 | 4.5E-03 | -0.83 |
table 2 - column D_ID is not in same order as table1
| D_ID | Value_2 | Value_1 | Value_3 |
| N6K_14 | 3.503 | 2.5E-03 | -3.48 |
| N6K_13 | 5.680 | 2.5E-03 | -1.63 |
| N6K_15 | 4.690 | 5.3E-03 | -0.63 |
| N6K_12 | 8.903 | 5.5E-03 | -4.83 |
Output table
| D_ID | Split | Value_1 | Value_2 | Value_3 |
| N6K_12 | Str | (1.503)- ( 5.5E-03) | | (-0.48)-( -4.83) |
| N6K_13 | Ref | (1.680)-( 2.5E-03) | | |
| N6K_14 | St | (1.690)-( 2.5E-03) | | |
| N6K_15 | Ref | | | (-0.83)-( -0.63) |

VulpesPosted May 15, 2014, 6:11 AM
DataTable table3 = table1.Copy();
foreach(DataRow row in table3.Rows)
{
string D_ID = row["D_ID"].ToString();
foreach(DataRow row2 in table2.Rows)
{
if (row2["D_ID"].ToString() == D_ID)
{
foreach(DataColumn col in table3.Columns)
{
if (col.ColumnName == "D_ID" || col.ColumnName == "Split") continue;
double val1 = double.Parse(row[col.ColumnName].ToString());
double val2 = double.Parse(row2[col.ColumnName].ToString());
row[col.ColumnName] = (val1 - val2).ToString();
}
}
}
}
VulpesPosted May 22, 2014, 9:29 AM
To round it, you can use:
Farhan ShariffPosted May 22, 2014, 9:15 AM
But How to round this val2 to 9 decimal places
double val2 = double.Parse(row2[col.ColumnName].ToString());
VulpesPosted May 22, 2014, 7:37 AM
row[col.ColumnName] = (val1 - val2).ToString();
since double values themselves don't know anything about formatting.
To display the difference to 9 decimal places, try:
row[col.ColumnName] = (val1 - val2).ToString("F9");
Farhan ShariffPosted May 22, 2014, 7:01 AM
double val1 = double.Parse(row[col.ColumnName].ToString());
double val2 = double.Parse(row2[col.ColumnName].ToString());
Can I round off the val1 and val2 to 9 sice they are exponential Values ....
I tried this but wont work
NumberStyles styles;
styles = NumberStyles.AllowExponent | NumberStyles.Number;
double val1 = double.Parse(row[col.ColumnName].ToString(),styles);
double val2 = double.Parse(row2[col.ColumnName].ToString(),styles);
Farhan ShariffPosted May 15, 2014, 10:51 AM
Farhan ShariffPosted May 15, 2014, 7:30 AM
VulpesPosted May 15, 2014, 6:58 AM
I know from Farhan's previous questions that he's creating these DataTables from CSV files rather than an RDBMS as I think you were assuming in your first post.
He'll probably then be displaying the data in a Chart control.
AFAICT a DataView would be no help here.
Anupam SinghPosted May 15, 2014, 6:17 AM
I think its good to use view instead of using code(C# ,VB or else.) , it will also improve the performance.
Anupam SinghPosted May 15, 2014, 6:04 AM
Farhan ShariffPosted May 15, 2014, 5:59 AM
Anupam SinghPosted May 15, 2014, 5:50 AM
you have to use join operator for these types of scenario :
Here is the query using join
Hope it will help.