1
Answer

Best way to read from Datatable and return JsonResult IN MVC

Anyone suggest me what is the best way to read bult data from DataTable and return as Json Multidimensional array to view...
 
I am using currently following... 
  1. public JsonResult GetAllCategory()  
  2. {
  3.     List<Dictionary<stringobject>> datarow = DataTableToList(new CategoryBAL().GetCategoryForEdit());  
  4.     new JavaScriptSerializer().Serialize(datarow);  
  5.     return Json(datarow, JsonRequestBehavior.AllowGet);  
  6. }  
 It will getting all rows from database and call the DataTableToList() to convert data into List<Dictionary<string,object>>
 
  1. public List<Dictionary<stringobject>> DataTableToList(DataTable dt)  
  2. {  
  3.     List<Dictionary<stringobject>> parentRow = new List<Dictionary<stringobject>>();  
  4.   
  5.     foreach (DataRow row in dt.Rows)  
  6.     {  
  7.         Dictionary<stringobject> childRow = new Dictionary<stringobject>();  
  8.         foreach (DataColumn col in dt.Columns)  
  9.         {  
  10.             childRow.Add(col.ColumnName, row[col]);  
  11.         }  
  12.         parentRow.Add(childRow);  
  13.     }  
  14.     return parentRow;  
  15. }  
Is there any other faster and optimized way to do this? 

Answers (1)