0

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.

1 Answer 1

2

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...

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Jignesh's feedback. I'm a newer for C#. I'm not full understanding your explanation. But I doubt that your feedback isn't compatible my requirement. According my requirement, the input data format is an array, like int16[] or int32[,]. And the output data format is DataTable. So if the input data is array, the code you said like "foreach (DataRow row in rowArray)" can't work.
hello @newcarcrazy What should i do for you ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.