newcarcrazy
newcarcrazy

Reputation: 105

Transform from array to data table

My requirement is that I want to transform a bunch of arrays into datatable. One array related on one datatable, and all the datatables are in a dataset. It seems the requirement is simple. But the difficult thing is that the array's dimension number isn't stable. It means maybe contain one dimension array and maybe contain two dimension array. So I want to use one function to handle two situation.

Currently, my solution is using overload to handle this situation. It seems a little heavy.

I define two generic methods, one is responsible for transforming one dimension array, the other is responsible for transforming two dimension array.

public DataTable GenerateTable<T>(T[,] array2Dim)

public DataTable GenerateTable<T>(T[] array1Dim)

Because the type of array maybe different, so I use generic in these two methods. I know this isn't the optimize operation way. So I want to know that whether has the more unify way of handling this situation.

Upvotes: 0

Views: 166

Answers (1)

Jignesh.Raj
Jignesh.Raj

Reputation: 5987

Why not iterate through your DataRow array and add (using DataRow.ImportRow, if necessary, to get a copy of the DataRow), something like:

 foreach (DataRow row in rowArray) {
       dataTable.ImportRow(row);
    }

Make sure your dataTable has the same schema as the DataRows in your DataRow array.

Happy Coding...

Upvotes: 2

Related Questions