ok i now it supose to be simple y have a multidimensional array, I try to fill my data table using the following code:
System.Data.DataTable _myDataTable =ne
How about an extension method
static class HappyExtEnding
{
public static DataTable ToDataTable(this T [] students)
{
if (students == null || students.Length == 0) return null;
DataTable table = new DataTable();
var student_tmp = students[0];
table.Columns.AddRange(student_tmp.GetType().GetFields().Select(field => new DataColumn(field.Name, field.FieldType)).ToArray());
int fieldCount = student_tmp.GetType().GetFields().Count();
students.All(student =>
{
table.Rows.Add(Enumerable.Range(0, fieldCount).Select(index => student.GetType().GetFields()[index].GetValue(student)).ToArray());
return true;
});
return table;
}
}
Usage
Student[] students = {
new Student { Id = 1, Name = "Joe Rattz", Address = "Sriram Apartments" },
new Student { Id = 6, Name = "Ulyses Hutchens", Address = "Sriram Apartments" },
new Student { Id = 19, Name = "Bob Tanko", Address = "Sriram Apartments" },
new Student { Id = 45, Name = "Erin Doutensal", Address = "Sriram Apartments" },
new Student { Id = 1, Name = "Joe Rattz", Address = "Sriram Apartments" },
new Student { Id = 12, Name = "Bob Mapplethorpe", Address = "Sriram Apartments" },
new Student { Id = 17, Name = "Anthony Adams", Address = "Sriram Apartments" },
new Student { Id = 32, Name = "Dignan Stephens Mark", Address = "Sriram Apartments" },
new Student { Id = 1232, Name = "Dignan Stephens", Address = "Sriram Apartments Henry Labamba Beligi" },
new Student { Id = 132, Name = "Neha Dhupia", Address = "Sriram Apartments 123456" },
new Student { Id = 132, Name = "", Address = "Sriram Apartments 123456" },
new Student { Id = 133, Name = "", Address = "Sriram Apartments 123456" },
new Student { Id = 134, Name = "Neha Dhupia", Address = "" },
new Student { Id = 134, Name = "Shradha Kapoor", Address = "Mumbai" }
};
//ParallelQuery
DataTable dtTmp = students.ToDataTable() ;