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


VulpesRank #55 · 29.2K reputationPosted May 15, 2014, 2:11 AM EDT
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();
}
}
}
}
Anupam SinghRank #643 · 3.7K reputationPosted May 15, 2014, 1:50 AM EDT
you have to use join operator for these types of scenario :
Here is the query using join
Hope it will help.
Farhan ShariffRank #2,004 · 725 reputationPosted May 15, 2014, 1:59 AM EDT
Anupam SinghRank #643 · 3.7K reputationPosted May 15, 2014, 2:04 AM EDT
Anupam SinghRank #643 · 3.7K reputationPosted May 15, 2014, 2:17 AM EDT
I think its good to use view instead of using code(C# ,VB or else.) , it will also improve the performance.
VulpesRank #55 · 29.2K reputationPosted May 15, 2014, 2:58 AM EDT
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.
Farhan ShariffRank #2,004 · 725 reputationPosted May 15, 2014, 3:30 AM EDT
Farhan ShariffRank #2,004 · 725 reputationPosted May 15, 2014, 6:51 AM EDT
Farhan ShariffRank #2,004 · 725 reputationPosted May 22, 2014, 3:01 AM EDT
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);
VulpesRank #55 · 29.2K reputationPosted May 22, 2014, 3:37 AM EDT
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 ShariffRank #2,004 · 725 reputationPosted May 22, 2014, 5:15 AM EDT
But How to round this val2 to 9 decimal places
double val2 = double.Parse(row2[col.ColumnName].ToString());
VulpesRank #55 · 29.2K reputationPosted May 22, 2014, 5:29 AM EDT
To round it, you can use: