datatable to object array c#

datatable to object array c#

 

public static object[,] Convert(DataTable dt)
{
    var rows = dt.Rows;
    int rowCount = rows.Count;
    int colCount = dt.Columns.Count;
    var result = new object[rowCount, colCount];

    for (int i = 0; i < rowCount; i++)
    {
        var row = rows[i];
        for (int j = 0; j < colCount; j++)
        {
            result[i, j] = row[j];
        }
    }

    return result;
}

 

List<object> lst = dt.AsEnumerable().ToList<object>();

 

public List<object> ShowDetails()
   {
       List<object> obj = new List<object>();

       DataTable dt = new DataTable();
       dt.Columns.Add("Id");
       dt.Columns.Add("Name");

       dt.Rows.Add("1","a");
       dt.Rows.Add("2", "b");
       dt.Rows.Add("3", "c");

       foreach (DataRow dr in dt.Rows)
       {

           obj.Add(dr);
       }

       return obj;
   }

2021-10-03