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...
- public JsonResult GetAllCategory()
- {
- List<Dictionary<string, object>> datarow = DataTableToList(new CategoryBAL().GetCategoryForEdit());
- new JavaScriptSerializer().Serialize(datarow);
- return Json(datarow, JsonRequestBehavior.AllowGet);
- }
It will getting all rows from database and call the DataTableToList() to convert data into List<Dictionary<string,object>>
- public List<Dictionary<string, object>> DataTableToList(DataTable dt)
- {
- List<Dictionary<string, object>> parentRow = new List<Dictionary<string, object>>();
-
- foreach (DataRow row in dt.Rows)
- {
- Dictionary<string, object> childRow = new Dictionary<string, object>();
- foreach (DataColumn col in dt.Columns)
- {
- childRow.Add(col.ColumnName, row[col]);
- }
- parentRow.Add(childRow);
- }
- return parentRow;
- }
Is there any other faster and optimized way to do this?