xxxxxxxxxx
public static DataTable ArrayToDatatable(Object[] objs)
{
var dt = new DataTable();
if (objs.Length == 0) return dt;
var objProps = objs[0].GetType().GetProperties();
for (var i = 0; i < objProps.Length; i++)
{
dt.Columns.Add(objProps[i].Name);
}
for (var i = 0; i < objs.Length; ++i)
{
DataRow row = dt.NewRow();
var objRow = objs[i];
for (var j = 0; j < objProps.Length; ++j)
{
row[j] = objProps[j].GetValue(objRow, null);
}
dt.Rows.Add(row);
}
return dt;
}
xxxxxxxxxx
string[] arrray = dt.Rows.OfType<DataRow>().Select(k => k[0].ToString()).ToArray();
xxxxxxxxxx
public static DataTable ArraytoDatatable(Object[,] numbers)
{
DataTable dt = new DataTable();
for (int i = 0; i < numbers.GetLength(1); i++)
{
dt.Columns.Add("Column" + (i + 1));
}
for (var i = 0; i < numbers.GetLength(0); ++i)
{
DataRow row = dt.NewRow();
for (var j = 0; j < numbers.GetLength(1); ++j)
{
row[j] = numbers[i, j];
}
dt.Rows.Add(row);
}
return dt;
}