4
Answers

Finding maximum and minimum Value column wise in datatable

Farhan Shariff

Farhan Shariff

10y
2.6k
1

I have two tables I want to compare them using Par Name column(here rows are compare with columns of table5) and find lowest and highest values 

table 1 
Par Name

SKIP

Lowest value
Highest
Value
D_Id
0


Split
0


Val_2
0






Val_1
0






Val_4
0









table5 
D_ID
Split
Val_1
Val_2
Val_4
N_01
Ret
-0.13672

-0.56202

-0.4211

N_92
Ret
-0.13767

-0.13911

-0.56752

N_03
Stress
-0.13711

-0.42072

-0.41932

N_98
Stress
-0.6132

-0.41979

-0.42025

N_56
Stress
-0.42026

-0.41955

-0.1367


  for (int q = 2; q < table1.Rows.Count; q++)
            {


                string colVal = table1.Rows[q]["Par Name"].ToString(); 
                if(table5.Columns.Contains(colVal))
                {

// Error : Specified cast is not valid
 table1.Rows[q]["Lowest value"] = table5.AsEnumerable().Min(r => r.Field<double>(colVal));
 table1.Rows[q]["Highest Value"] = table5.AsEnumerable().Max(r => r.Field<double>(colVal));
                }
            }




output table


Par Name

SKIP

Lowest value
Highest
Value
D_Id
0


Split
0


Val_2
0
-0.56202

-0.13911

Val_1
0
-0.6132

-0.13672

Val_4
0
-0.56752

-0.1367

Answers (4)