Sanjay gautam

Sanjay gautam

  • NA
  • 90
  • 14.4k

convert loop to linq

Oct 21 2019 11:25 PM
How to convert foreach loop in linq there is any esay way to change loop to linq i have some code showen blewo
  1. static public string table2Json(DataSet ds, int table_no)  
  2. {  
  3.      
  4.     try  
  5.     {  
  6.         object[][] tb = new object[ds.Tables[table_no].Rows.Count][];  
  7.   
  8.         int r = 0;  
  9.         foreach (DataRow dr in ds.Tables[table_no].Rows)  
  10.         {  
  11.             tb[r] = new object[ds.Tables[table_no].Columns.Count];  
  12.             int col = 0;  
  13.   
  14.             foreach (DataColumn column in ds.Tables[table_no].Columns)  
  15.             {  
  16.                 tb[r][col] = dr[col];  
  17.                 if ((tb[r][col]).Equals(System.DBNull.Value))  
  18.                 {  
  19.                     tb[r][col] = "";  
  20.                 }  
  21.                 col++;  
  22.             }  
  23.             r++;  
  24.         }  
  25.        string table = JsonConvert.SerializeObject(tb, Formatting.Indented);  
  26.         return table;  
  27.     }  
  28.     catch (Exception ex)  
  29.     {  
  30.         tools.log(ex.Message);  
  31.         throw ex;  
  32.     }  
  33. }  

Answers (3)