3
Answers

Max/Mini Value in datatable by comparing with other datatabl

I have two datatables table1 and table 5
Table1
Par Name  
Lowest  
Highest  
 lower_limit  
 upper_limit  
Con_3


-0.65

0.03

Con_2


-0.12

0.06

Con_1


-0.41

0.02


Table5
Con_1
Con_3
Con_2


Split
Die_ID
Normal_Inverse
0.0013

0.0018

0.0012    


Stressed

N6K83_12

-2.49897
 
-0.0071  

0.0015  

-0.0005



Ref
N634-10_12

-2.16681
 
-0.0004

0.082

-0.0027



Stressed

N610_15

-1.97699
 


I want to compare lower limit and upper limit from table 1 with corresponding column (Con_1,Con_2,Con_3  from table 5 ) and delete the column in table5 if :

There is no value lesser then lower_limit OR there is no value greater than upper_limit in the table5 column



Answers (3)
0
Vulpes

Vulpes

NA 98.3k 1.5m 10y
Assuming all numeric columns are of type double, then try:

   foreach(DataRow dr in table1.Rows)
   {
      string colName = dr["Par Name"].ToString();
      if(table5.Columns.Contains(colName))
      {
         double lower = (double)dr["lower_limit"];
         double upper = (double)dr["upper_limit"];
         double min = table5.Rows.Cast<DataRow>().Min(r => r.Field<double>(colName));
             
         if (min >= lower)
         {
            double max = table5.Rows.Cast<DataRow>().Max(r => r.Field<double>(colName));
            if (max <= upper) table5.Columns.Remove(colName);
         }
      }
   }

or, if they're all strings, try:

   foreach(DataRow dr in table1.Rows)
   {
      string colName = dr["Par Name"].ToString();
      if(table5.Columns.Contains(colName))
      {
         double lower = double.Parse(dr["lower_limit"].ToString());
         double upper = double.Parse(dr["upper_limit"].ToString());
         double min = table5.Rows.Cast<DataRow>().Min(r => double.Parse(r.Field<string>(colName)));
             
         if (min >= lower)
         {
            double max = table5.Rows.Cast<DataRow>().Max(r => double.Parse(r.Field<string>(colName)));
            if (max <= upper) table5.Columns.Remove(colName);
         }
      }
   }

Accepted
0
Farhan Shariff

Farhan Shariff

NA 1.1k 111.4k 10y
Thank you vulpes . works perfect only  thing is I had to remove the last three columns from table5 (Split , Die_id and normal inverse), since I am adding it later again no problem :)
0
Khan Abrar Ahmed

Khan Abrar Ahmed

NA 5.8k 200k 10y
Hi,
what basis u want to match the rows?? please explain little bit more.